Explicit template specialization for constructor

旧街凉风 提交于 2020-01-01 19:14:12

问题


I have a template class, with a copy constructor:

struct index_method {/*whatever*/};

template <class A, class B>
class ExampleClass
{
public:
   ExampleClass(void) {}
   template <class C>
   ExampleClass( const ExampleClass<A,C>& src_, const B& b_ = B() ) : _b(b_) { }
private:
   B _b;
};

The following template constructor specialization is compiled properly by gcc 4.7.0:

template <>
template <>
ExampleClass<double,index_method>::ExampleClass<index_method>( const ExampleClass<double,index_method>& src_, const index_method& b_ )
  : _b(b_)
{
}

But it has issues in MSVC:

error C2976: 'ExampleClass' : too few template arguments

Based on another topic, I tried a more simple code just for MSVC:

ExampleClass<double,index_method>::ExampleClass<index_method>( const ExampleClass<double,index_method>& src_, const index_method& method_ )
  : _b(method_)
{
}

but it also doesn't work.

Is there any way to specify a template copy constructor for a template class in MSVC 2012?


回答1:


I have no idea why so, since gcc compiles it, but clang reject as MSVC, but with another error. However, you can simply use following code

struct index_method {/*whatever*/};

template <class A, class B>
class ExampleClass
{
public:
ExampleClass(void) {}
template <class C>
ExampleClass( const ExampleClass<A,C>& src_, const B& b_ = B() ) : _b(b_) { }
private:
B _b;
};

template <>
template <>
ExampleClass<double,index_method>::ExampleClass
( const ExampleClass<double,index_method>& src_, const index_method& b_ )
: _b(b_)
{
}

Example



来源:https://stackoverflow.com/questions/19562680/explicit-template-specialization-for-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!