具有模板类参数的模板类专业化 [英] template class specialization with template class parameter

查看:30
本文介绍了具有模板类参数的模板类专业化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有:

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

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

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

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.

使用 Visual Studio 的编译器,我得到:

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

我找到了这篇文章:模板类专业化,其中模板参数是一个模板

所以我按照建议尝试了完整的类专业化,但它也不起作用:

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

视觉说:

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

欢迎任何帮助!

浮出水面.

推荐答案

删除多余的 template<> 就可以了.

Remove the extra template<> and it will work.

这篇关于具有模板类参数的模板类专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆