简体   繁体   English

具有多种类型的类的模板专业化

[英]Template specialization for a class which has multiple types

Class declaration is in a class.h file : 类声明在class.h文件中:

template <typename K, typename T>
class classx
{
  ...
  unsigned int func1(K key);
  ...

which includes this class.hpp : 其中包括这个class.hpp:

template <typename K, typename T>
unsigned int classx<K,T>::func1(K key)
{
    return 1;
}


//Func1 for <int, typename T>       ????

template <>
template <typename T>
unsigned int classx<int,T>::func1<int, T>(int key)  // ERROR!
{
    return 1;
}

This results: 结果:

error: expected initializer before '<' token 错误:“ <”令牌之前的预期初始化程序

What is the proper way of doing this? 正确的做法是什么?

Remove the second set of template parameters from your func1 definition and the template <> : func1定义和template <>删除第二组模板参数:

template <typename T>
unsigned int classx<int,T>::func1(int key)
{
    return 1;
}

EDIT: 编辑:

Additionally, you cannot partially specialize a single function of a template class . 此外, 您不能部分专门化模板类的单个功能 If you wish to do so, you will have to partially specialize the entire class. 如果您愿意,您将必须对整个班级进行部分专业化培训。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM