构造函数的显式模板专门化 [英] Explicit template specialization for constructor

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

问题描述

我有一个模板类,有一个复制构造函数:

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

以下模板构造函数专门化由gcc 4.7.0正确编译:

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_)
{
}

但在MSVC中有问题:

But it has issues in MSVC:


错误C2976:'ExampleClass':模板参数过少

基于另一个 topic ,我试过一个更简单的代码只为MSVC:

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_)
{
}

但它也不起作用。

有没有办法在MSVC 2012中为模板类指定模板副本构造函数?

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

推荐答案

想法为什么这样,因为gcc编译它,但clang拒绝作为MSVC,但有另一个错误。
然而,你可以简单地使用下面的代码

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_)
{
}

示例

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

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