Jac*_*son 11 c++ clang explicit-instantiation clang++
我不确定它是否是Clang 3.2中的错误或违反C++ 03,但似乎模板类的模板化构造函数的显式实例化失败,但模板类的模板化成员函数的显式实例化成功.
例如,以下编译没有clang ++和g ++的问题:
template<typename T>
class Foo
{
public:
template<typename S>
void Bar( const Foo<S>& foo )
{ }
};
template class Foo<int>;
template class Foo<float>;
template void Foo<int>::Bar( const Foo<int>& foo );
template void Foo<int>::Bar( const Foo<float>& foo );
template void Foo<float>::Bar( const Foo<int>& foo );
template void Foo<float>::Bar( const Foo<float>& foo );
Run Code Online (Sandbox Code Playgroud)
而以下编译没有使用g ++警告但使用clang ++失败:
template<typename T>
class Foo
{
public:
template<typename S>
Foo( const Foo<S>& foo )
{ }
};
template class Foo<int>;
template class Foo<float>;
template Foo<int>::Foo( const Foo<int>& foo );
template Foo<int>::Foo( const Foo<float>& foo );
template Foo<float>::Foo( const Foo<int>& foo );
template Foo<float>::Foo( const Foo<float>& foo );
Run Code Online (Sandbox Code Playgroud)
特别是,我看到表单的两个错误消息:
TemplateMember.cpp:12:20: error: explicit instantiation refers to member
function 'Foo<int>::Foo' that is not an instantiation
template Foo<int>::Foo( const Foo<int>& foo );
^
TemplateMember.cpp:9:16: note: explicit instantiation refers here
template class Foo<int>;
^
Run Code Online (Sandbox Code Playgroud)
这是违反标准还是clang ++中的错误?
看起来你发现了一个GCC错误.这两个都命名隐式声明的复制构造函数:
template Foo<int>::Foo( const Foo<int>& foo );
template Foo<float>::Foo( const Foo<float>& foo );
Run Code Online (Sandbox Code Playgroud)
Per [temp.explicit] p4,
如果显式实例化的声明命名了一个隐式声明的特殊成员函数(第12条),那么该程序就是格式错误的.
因此,Clang拒绝此代码是正确的.