1

在 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)

有这样的限制的原因是什么?

4

1 回答 1

3

它与成员函数定义的正常工作方式一致。您只能定义您首先声明的函数。例如,这不会编译:

struct B {
};

B::B() {
}

构造函数是隐式声明的,因此不能显式定义。

您引用的段落说这对于模板专业化同样适用。

于 2015-01-07T00:18:40.450 回答