在c ++中继承模板类的构造函数 [英] Inheriting constructors of template class in c++

查看:148
本文介绍了在c ++中继承模板类的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c ++ 11中,可以继承构造函数。我有以下代码(再现示例),无法编译:

In c++11 it is possible to inherit constructors. I have the following code (reproducing example), which fails to compile:

template<class T> class A
{
    ...
}

template<class T> class B : public A<T>
{
    public:       
        B(int a, int b = 0);
}
template<class T> B::B(int a, int b /* =0 */)
{
    ...
}

class C : public B<int>
{
    public: 
    using B::B;
}

以下代码不会运行 C 没有构造函数,即 B 不能继承:

The following code doesn't run with the error that C doesn't have the constructor, i.e. the one from B is not inherited:

C obj(5);
//or
C obj(5, 3);


推荐答案

Visual Studio 2013不 >支持继承构造函数。有关详细信息,请参见支持C ++ 11功能。以下是相关部分:

Visual Studio 2013 does not support inheriting constructors. See Support For C++11 Features for more information. Here's the relevant part:


C ++ 11核心语言特性

C++11 Core Language Features


  • 继承构造函数

    • Visual Studio 2010:否

    • Visual Studio 2012:否

    • Visual Studio 2013:否

    更新

    看起来支持继承构造函数在2013年11月CTP中添加(帽子提示:@yngum)

    Looks like support for inheriting constructors was added in the November 2013 CTP (hat tip: @yngum)

    这篇关于在c ++中继承模板类的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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