模板类的模板化构造函数的显式实例化 [英] Explicit instantiation of templated constructor for template class

查看:139
本文介绍了模板类的模板化构造函数的显式实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定是否是Clang 3.2中的错误或违反C ++ 03,但是对于模板类的模板化构造函数的显式实例化失败了,但模板类的模板成员函数的显式实例化成功。 / p>

例如,下面的编译没有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 ++失败:

 模板< 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是指成员
函数'Foo< int> :: Foo' b $ b template Foo< int> :: Foo(const Foo< int>& foo);
^
TemplateMember.cpp:9:16:note:显式实例化在这里
模板类Foo< int> ;;
^

这是违反标准还是clang ++中的错误?

解决方案

看起来您发现了一个GCC错误。这两个名称都是隐含声明的复制构造函数:

  template Foo  template Foo< float> :: Foo(const Foo< float>& foo); 

每个[temp.explicit] p4,



< blockquote>

如果显式实例化的声明命名一个隐含声明的特殊成员函数(第12条),程序就会生成错误。


因此,Clang拒绝此代码是正确的。


I am uncertain if it is a bug in Clang 3.2 or a violation of C++03, but it appears that explicit instantiation of templated constructors for template classes fails, but explicit instantiation of templated member functions of template classes succeeds.

For instance, the following compiles without a problem with both clang++ and 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 );

whereas the following compiles without warning with g++ but fails with 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 );

In particular, I see two error messages of the form:

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

Is this a violation of the standard or a bug in clang++?

解决方案

It looks like you've found a GCC bug. These both name the implicitly-declared copy constructor:

template Foo<int>::Foo( const Foo<int>& foo );
template Foo<float>::Foo( const Foo<float>& foo );

Per [temp.explicit]p4,

If the declaration of the explicit instantiation names an implicitly-declared special member function (Clause 12), the program is ill-formed.

Therefore Clang is correct to reject this code.

这篇关于模板类的模板化构造函数的显式实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆