问题
Say I have :
template < typename T >
class ClassA
{
void doTheStuff (T const * t);
};
template < typename T >
class ClassB
{
// Some stuff...
};
I'd like to specialize the doTheStuff method for all instances of the ClassB template like this:
template <typename T>
void ClassA< ClassB< T > >::doTheStuff (ClassB< T > const * t)
{
// Stuff done in the same way for all ClassB< T > classes
}
But of course, this doesn't work. Shame is I don't know how I could do that.
With visual studio's compiler I get:
error C2244: 'ClassA<T>::doTheStuff' : unable to match function definition to an existing declaration
see declaration of 'ClassA<T>::doTheStuff'
definition
'void ClassA<ClassB<T>>::doTheStuff(const ClassB<T> *)'
existing declarations
'void ClassA<T>::doTheStuff(const T *)'
I found this post: Templated class specialization where template argument is a template
So I tried the full class specialization as advised but it doesn't work either:
template <>
template < typename U >
class ClassA< ClassB< U > >
{
public:
void doTheStuff (ClassB< U > const * b)
{
// Stuff done in the same way for all ClassB< T > classes
}
};
Visual says:
error C2910: 'ClassA<ClassB<U>>' : cannot be explicitly specialized
Any help welcome !
Floof.
回答1:
Remove the extra template<>
and it will work.
来源:https://stackoverflow.com/questions/13970407/template-class-specialization-with-template-class-parameter