在c ++ iso 2003/2011 [temp.expl.spec] / 4中写了
类模板的成员函数,成员类或静态数据成员可以显式专门用于隐式实例化的类特化;在这种情况下,类模板的定义应在声明类模板成员的显式特化的范围内。如果类模板成员的这种显式特化名称是一个隐式声明的特殊成员函数(第12条),那么该程序就是格式错误。
据我所知,应该在显式特化之前定义允许专用的特殊函数。
template <typename T>
class A
{
public:
A()
{ /* some definition */}
};
template <>
A<int>::A()
{ /*explicit specialization def body*/} // this is OK
但
template <typename T>
class B
{};
template <>
B<int>::B()
{ /*explicit specializationdef body */} // this is forbidden by ISO c++
// and when compiling with VS2013 gives compile error
// cannot define a compiler-generated special member
// function (must be declared in the class first)
有这种限制的原因是什么?
答案 0 :(得分:3)
这与成员函数的定义通常的工作方式一致。您只能定义首先声明的函数。例如,这将无法编译:
struct B {
};
B::B() {
}
构造函数是隐式声明的,因此无法显式定义。
您引用的段落表示,对于模板专业化,它的作用相同。