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

编程入门 行业动态 更新时间:2024-10-27 07:24:57
本文介绍了具有模板类参数的模板类专业化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

说我有:

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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 06:34:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405848.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   类专业   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!