template class specialization with template class parameter

◇◆丶佛笑我妖孽 提交于 2019-12-30 11:11:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!