C++ 模板类的模板化构造函数的显式实例化
我不确定这是Clang3.2中的错误还是违反了C++03,但似乎模板类的模板构造函数的显式实例化失败了,但模板类的模板成员函数的显式实例化成功了 例如,以下代码在编译时不会出现clang++和g++的问题:C++ 模板类的模板化构造函数的显式实例化,c++,clang,clang++,explicit-instantiation,C++,Clang,Clang++,Explicit Instantiation,我不确定这是Clang3.2中的错误还是违反了C++03,但似乎模板类的模板构造函数的显式实例化失败了,但模板类的模板成员函数的显式实例化成功了 例如,以下代码在编译时不会出现clang++和g++的问题: template<typename T> class Foo { public: template<typename S> void Bar( const Foo<S>& foo ) { } }; template clas
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 );
模板
福班
{
公众:
模板
空栏(常量Foo和Foo)
{ }
};
模板类Foo;
模板类Foo;
模板void Foo::Bar(const Foo&Foo);
模板void Foo::Bar(const Foo&Foo);
模板void Foo::Bar(const Foo&Foo);
模板void Foo::Bar(const Foo&Foo);
然而,以下使用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 );
模板
福班
{
公众:
模板
Foo(const Foo&Foo)
{ }
};
模板类Foo;
模板类Foo;
模板Foo::Foo(constfoo&Foo);
模板Foo::Foo(constfoo&Foo);
模板Foo::Foo(constfoo&Foo);
模板Foo::Foo(constfoo&Foo);
特别是,我看到表单中的两条错误消息:
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>;
^
TemplateMember.cpp:12:20:错误:显式实例化引用了成员
不是实例化的函数“Foo::Foo”
模板Foo::Foo(constfoo&Foo);
^
TemplateMember.cpp:9:16:注意:这里引用显式实例化
模板类Foo;
^
这是违反标准还是clang++中的错误?看起来您发现了一个GCC错误。这两个函数都将隐式声明的复制构造函数命名为:
template Foo<int>::Foo( const Foo<int>& foo );
template Foo<float>::Foo( const Foo<float>& foo );
模板Foo::Foo(constfoo&Foo);
模板Foo::Foo(constfoo&Foo);
根据[temp.explicit]p4
如果显式实例化的声明指定了一个隐式声明的特殊成员函数(第12条),则程序是格式错误的
因此,Clang拒绝此代码是正确的。看起来像是有效的C++03。可能是叮当声中的bug++谢谢你Richard!你完全正确。通过显式声明构造函数Foo(constfoo&Foo)并删除复制构造函数的显式实例化,程序随后使用Clang进行编译。