I have some C++ classes that all have the same template parameters
template <typename T, size_t i>
struct A {
};
template <typename T, size_t i>
struct B : A<T,i>{
};
template <typename T, size_t i>
struct C : A<T,i>{
};
and so on. I also have a series of methods that will work on any of these classes. However, the problem is in the return type. I would like this method to return an instance of the passed in class, with the integer decremented by one. For instance, if I just overload the function, that would look like this
template <typename T, size_t i>
A<T,i-1> func(const A<T,i> & a){
}
template <typename T, size_t i>
B<T,i-1> func(const B<T,i> & a){
}
template <typename T, size_t i>
C<T,i-1> func(const C<T,i> & a){
}
Is there a way to accomplish this without overloading the function for each type? By that I mean... is it possible to replace these with a single templated function? The logic is identical for all of the functions.
I imagine that that would look something like
template <typename P, size_t i>
P<i-1> func( const P<i> & p ){
}
where P
somehow captures the original type A
, B
, or C
, as well as the inner type T
.
Or, if you think CRTP is the way to go, then how would I structure that?
Aucun commentaire:
Enregistrer un commentaire