加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 模板类的模板构造函数的显式实例化

发布时间:2020-12-16 05:56:12 所属栏目:百科 来源:网络整理
导读:我不确定它是否是Clang 3.2中的错误或者是C03的错误,但是模板类的模板化构造函数的显式实例化似乎失败了,但模板类的模板化成员函数的显式实例化成功. 例如,以下编译与clang和g都没有问题: templatetypename Tclass Foo{public: templatetypename S void Bar
我不确定它是否是Clang 3.2中的错误或者是C03的错误,但是模板类的模板化构造函数的显式实例化似乎失败了,但模板类的模板化成员函数的显式实例化成功.

例如,以下编译与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但与cl声失败:

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>;
               ^

这是违反标准还是cl a的错误?

解决方法

看起来你发现了一个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是拒绝这个代码的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读