问题
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