模板类专门化,其中模板参数是模板 [英] Templated class specialization where template argument is a template

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

问题描述

我想知道是否可能有类似的东西。基本上,我有一个模板类,偶尔采取模板类的对象。我想专门化它(或只是一个成员函数)为一个特定的模板类,但该类的'通用'形式。

  template< typename T,typename S> 
class SomeRandomClass
{
//放在这里
};

template< typename T>
class MyTemplateClass
{
void DoSomething(T& t){
//...something
}
};

模板<>
void MyTemplateClass< SomeRandomClass< > :: DoSomething(SomeRandomClass& t)
{
//这里特别发生的事情
}
pre>

用适当的类型(double等)替换问号,但我想它仍然是通用的。我不知道什么放在那里,因为任何类型都不会被定义。我已经环顾四周,了解了模板模板参数,并尝试了各种组合无法使用。感谢您的帮助!

解决方案

可以像这样专门化类

 模板<> 
template< typename T,typename S>
class MyTemplateClass< SomeRandomClass< T,S> >
{
void DoSomething(SomeRandomClass< T,S&&t)  {/ * something * /}
};

不可能专门化成员方法,因为专业化是整个类的,并且你必须定义一个新类。但您可以

 模板<> 
template< typename T,typename S>
class MyTemplateClass< SomeRandomClass< T,S> >
{
void DoSomething(SomeRandomClass< T,S& t);
};

模板<>
template< typename T,typename S>
void MyTemplateClass< SomeRandomClass< T,S> > :: DoSomething(SomeRandomClass< T,S&&t)
{
// something
}

分割声明和定义。


I wondering if something similar to this is possible. Basically, I have a templated class that occasionally takes objects of templated classes. I would like to specialize it (or just a member function)for a specific templated class, but the 'generic' form of that class.

template<typename T, typename S>
class SomeRandomClass
{
    //put something here
};

template<typename T>
class MyTemplateClass
{
    void DoSomething(T & t) {
       //...something
    }
};

template<>
void MyTemplateClass< SomeRandomClass<???> >::DoSomething(SomeRandomClass<???> & t)
{
    //something specialized happens here
}

Replacing the question marks with appropriate types (double, etc) works, but I would like it to remain generic. I don't know what to put there, as any types wouldn't have been defined. I've looked around, and learned about template template parameters, and tried various combinations to no avail. Thanks for the help!

解决方案

It's possible to specialize the class like this

template <>
template <typename T,typename S>
class MyTemplateClass <SomeRandomClass<T,S> >
{
    void DoSomething(SomeRandomClass<T,S>& t) { /* something */ }
};

It's not possible to specialize just the member method, because the specialization is on the class as a whole, and you have to define a new class. You can, however, do

template <>
template <typename T,typename S>
class MyTemplateClass <SomeRandomClass<T,S> >
{
    void DoSomething(SomeRandomClass<T,S>& t);
};

template <>
template <typename T,typename S>
void MyTemplateClass<SomeRandomClass<T,S> >::DoSomething(SomeRandomClass<T,S>& t)
{
    // something
}

to split up the declaration and definition.

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

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