【发布时间】:2015-10-17 02:56:57
【问题描述】:
我有一个模板类和一个模板成员函数:
template<class T1>
struct A{
template<class T2>
static int f(){return 0;}
};
我想专攻T1和T2相同的情况,
例如,为任何T 定义大小写A<T>::f<T>。
但我找不到实现此目的的关键字组合。
如何部分(?)专门化模板类和模板静态函数的组合?
这些是我的不成功尝试,以及错误消息:
1) 专攻类内:fatal error: cannot specialize a function 'f' within class scope)
template<class T1>
struct A{
template<class T2>
static int f(){return 0;}
template<>
static int f<T1>(){return 1;}
};
2) 课外“同时”专精:fatal error: cannot specialize a member of an unspecialized template
template<class T>
void A<T>::f<T>(){return 1;}
3) 专门使用template<>: fatal error: too few template parameters in template redeclaration
template<> template<class T>
void A<T>::f<T>(){return 1;}
4) 倒序:fatal error: cannot specialize (with 'template<>') a member of an unspecialized template
template<class T> template<>
void A<T>::f<T>(){return 1;}
5) 特化整个类(基于尝试3的错误):fatal error: class template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list
template<class T>
struct A<T>{
template<>
static int f<T>(){return 1;}
};
6) 专门化类但不专门化函数 (?):fatal error: too few template parameters in template redeclaration
template<> template<class T1>
template<> template<class T>
int A<T>::f(){return 0;}
我使用clang 3.5 C++14 生成错误消息。
【问题讨论】:
标签: c++ templates c++14 template-specialization partial-specialization