繁体   English   中英

类模板特殊成员函数显式特化

[英]class template special member function explicit specialization

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

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

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

struct B {
};

B::B() {
}

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

您引用的段落表示,对于模板特化,这同样适用。

暂无
暂无

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

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