简体   繁体   English

构造函数的显式模板特化

[英]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: 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: 但它在MSVC中存在问题:

error C2976: 'ExampleClass' : too few template arguments 错误C2976:'ExampleClass':模板参数太少

Based on another topic , I tried a more simple code just for MSVC: 根据另一个主题 ,我为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? 有没有办法在MSVC 2012中为模板类指定模板复制构造函数?

I have no idea why so, since gcc compiles it, but clang reject as MSVC, but with another error. 我不知道为什么会这样,因为gcc编译它,但clang拒绝作为MSVC,但有另一个错误。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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