【问题标题】:template class specialization with template class parameter带有模板类参数的模板类特化
【发布时间】:2012-12-07 20:51:24
【问题描述】:

说我有:

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

欢迎任何帮助!

浮空。

【问题讨论】:

    标签: c++ templates specialization


    【解决方案1】:

    删除多余的template&lt;&gt; 即可。

    【讨论】:

    • 谢谢!它确实有效,我不知道他们为什么在另一个线程上有这个额外的模板。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 2018-11-26
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    相关资源
    最近更新 更多