简体   繁体   中英

Explicit template specialization for constructor

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM