作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
不确定是Clang 3.2的bug还是违反了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 );
而以下代码使用 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 );
特别是,我看到了以下形式的两条错误消息:
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>;
^
这是否违反了标准或 clang++ 中的错误?
最佳答案
您似乎发现了一个 GCC 错误。这些都命名了隐式声明的复制构造函数:
template Foo<int>::Foo( const Foo<int>& foo );
template Foo<float>::Foo( const Foo<float>& foo );
根据 [temp.explicit]p4,
If the declaration of the explicit instantiation names an implicitly-declared special member function (Clause 12), the program is ill-formed.
因此 Clang 拒绝此代码是正确的。
关于c++ - 模板类的模板化构造函数的显式实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15045879/
我是一名优秀的程序员,十分优秀!