模板类参数的模板类专业化

时间:2012-12-20 10:46:37

标签: c++ templates specialization

说我有:

template < typename T >
class ClassA 
{
    void doTheStuff (T const * t);
};

template < typename T >
class ClassB
{
    // Some stuff...
};

我想为所有ClassB模板实例专门化doTheStuff方法,如下所示:

template <typename T>
void ClassA< ClassB< T > >::doTheStuff (ClassB< T > const * t)
{
    // Stuff done in the same way for all ClassB< T > classes
}

但当然,这不起作用。羞耻的是我不知道我怎么能这样做。

使用visual studio的编译器,我得到:

 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 *)'

我发现了这篇文章:Templated class specialization where template argument is a template

所以我按照建议尝试了全班专业化,但它也不起作用:

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
    }
};

视觉说:

error C2910: 'ClassA<ClassB<U>>' : cannot be explicitly specialized

欢迎任何帮助!

Floof。

1 个答案:

答案 0 :(得分:2)

删除额外的template<>,它会起作用。