Override template function based on what class the type is derived from
Here's a simpler example first to better explain what i mean.
template <typename T>
class TemplateClass
{
public:
void print();
};
template <>
void TemplateClass<int>::print()
{
std::cout << "T is an int!" << std::endl;
}
template <>
void TemplateClass<float>::print()
{
std::cout << "T is a float!" << std::endl;
}
int main()
{
TemplateClass<int> i;
i.print();
TemplateClass<float> f;
f.print();
system("PAUSE");
}
Here i override the print() function based on if T is int or float.
The program prints:
T is an int!
T is a float!
Press any key to continue . . .
What i actually want is to do this kind of overriding based on which class T is DERIVED from, where the classes T can be derived from is other template classes as they use the curiously recursive template pattern.
What i tried is this and i cant think of any other way
template <typename T>
class BaseOne {};
template <typename T>
class BaseTwo {};
struct DerivedOne : public BaseOne<DerivedOne>
{
int i;
};
struct DerivedTwo : public BaseTwo<DerivedTwo>
{
float x, y, z;
};
template <typename T>
class TemplateClass
{
public:
void print();
};
template <typename U>
void TemplateClass<BaseOne<U>>::print()
{
std::cout << "U is derived from BaseOne!" << std::endl;
}
template <typename U>
void TemplateClass<BaseTwo<U>>::print()
{
std::cout << "U is derived from BaseTwo!" << std::endl;
}
int main()
{
TemplateClass<DerivedOne> d1;
d1.print();
TemplateClass<DerivedTwo> d2;
d2.print();
system("PAUSE");
}
The program should print:
U is derived from BaseOne!
U is derived from BaseTwo!
Press any key to continue . . .
But instead i get the errors
// I believe this is the main error
Error (active) E0498 template argument list must match the parameter list
// And these errors comes as a result of the error above
Error C2039 'print': is not a member of 'TemplateClass<BaseOne/BaseTwo<T>>'
// BaseOne/BaseTwo on respective lines in code
Error C2447 '{': missing function header (old-style formal list?)
On the lines
void TemplateClass<BaseOne<U>>::print()
and
void TemplateClass<BaseTwo<U>>::print()
Im kind of lost, would appreciate some input!
Edit: Solved, see comments.
Posted May 9, 2019, 6:11 PM UTC · edited
65
Do you really need overriding/specialization? You can use
if constexprin C++17:If you really need overriding/specialization, use
std::enable_if(SFINAE)I guess i could!
I thought something like what i described might be more commonly used for some reason, as it kind of "makes sense" to write it that way for me.
This seems like a better solution though, as i wont have to repeat much code writing two functions that are almost the same with just a few differences.
Thank you!
There is a
is_base_oftype trait.std::is_base_of_v<BaseOne<T>, T>.Also,
static_assert(false)will never compile on GCC and Clang. Making the condition dependent on the type argument will work.static_assert(sizeof(T) < 0)Thank you! Will definitely use this!
from cppreference:
And sorry for
static_assert(false).