I understand how to create type traits and then specialise for a particular class, but in my case I would like to specialise for a class template. The code below does not compile, but the idea is that the specialisation of Traits for MyTemplatisedClass should work for which ever type the user decides to use with MyTemplatisedType.
class Traits
{
public:
static bool someProperty(void) { return false; }
};
template<typename Type>
class MyTemplatisedClass
{
};
template<typename Type>
template<>
class Traits< MyTemplatisedClass<Type> >
{
public:
static bool someProperty(void) { return true; }
};
int main(int argc, char* argv[])
{
std::cout << Traits< MyTemplatisedClass<float> >::someProperty() <<std::endl; //This should be true
return 0;
}
Is this possible or am I asking too much? According to the compiler the first problem is
error C2989: 'Traits' : class template has already been declared as a non-class template
Which is correct, but how do I fix this? If it makes any difference I don’t need it to work for non-templatised classes, just templatised ones is fine. Edit: Actually it would be nice if it worked for both templatised and non-templatised classes.
Traitsneeds to be a template in order to be specialized.In the specialization drop the line with empty
<>: this isn’t a template nested within a template or something.But if you meant the specialization for any template with one type argument, then that would be: