继承构造函数和大括号或初始值 [英] Inheriting constructors and brace-or-equal initializers

查看:210
本文介绍了继承构造函数和大括号或初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么你不能编译一个类,它有一个成员(不是默认可构造)与一个大括号初始值设定器和一个继承的构造函数。 g ++说:

I don't understand why you can't compile a class which has both a member (not default constructible) with a brace-or-equal initializer and an inherited constructor. g++ says :


test.cpp:22:15:error:使用已删除的函数'Derived :: Derived >
派生d(1.2f);

test.cpp:22:15: error: use of deleted function ‘Derived::Derived(float)’
Derived d(1.2f);

test.cpp:16:13:note:'Derived :: Derived(float)'被隐式删除

,因为默认定义是错误:

使用Base :: Base;

test.cpp:16:13: note: ‘Derived::Derived(float)’ is implicitly deleted
because the default definition would be ill-formed:
using Base::Base;

test.cpp:16:13:错误:没有匹配函数调用'NoDefCTor :: NoDefCTor()'

test.cpp: 5:1:note:candidate:

NoDefCTor :: NoDefCTor(int)NoDefCTor(int){}

test.cpp:16:13: error: no matching function for call to ‘NoDefCTor::NoDefCTor()’
test.cpp:5:1: note: candidate:
NoDefCTor::NoDefCTor(int) NoDefCTor(int) {}

无法编译的代码(在g ++ 5.1下):

Code that fails to compile (under g++ 5.1):

struct NoDefCTor
{
    NoDefCTor(int) {}
};

struct Base
{
    Base(float) {}
};

struct Derived : Base
{
    using Base::Base;
    NoDefCTor n2{ 4 };
};

int main()
{
    Derived d(1.2f);
}

编译代码,但从不使用 NoDefCTor 的默认构造函数(尽管显然需要它!):

Code that compiles, but never uses NoDefCTor's default constructor (despite apparently needing it!):

struct NoDefCTor
{
    NoDefCTor(int) {}
    NoDefCTor() = default;
};

struct Base
{
    Base(float) {}
};

struct Derived : Base
{
    using Base::Base;
    NoDefCTor n2{ 4 };
};

int main()
{
    Derived d(1.2f);
}



我真的不喜欢有一个默认构造函数的想法,不需要一个。在一个方面,两个版本编译(和行为)只是罚款MSVC14。

I don't really like the idea of having a default constructor when I don't need one. On a side note both versions compile (and behave) just fine on MSVC14.

推荐答案

这是一个 gcc bug,#67054 。将alltaken380的错误报告与OP的情况进行比较:

This is a gcc bug, #67054. Comparing the bug report by alltaken380 to the OP's case:

// gcc bug report                        // OP
struct NonDefault                        struct NoDefCTor
{                                        {
    NonDefault(int) {}                       NoDefCTor(int) {}
};                                       };

struct Base                              struct Base
{                                        {
    Base(int) {}                             Base(float) {}
};                                       };

struct Derived : public Base             struct Derived : Base
{                                        {
    NonDefault foo = 4;                      NoDefCTor n2{ 4 };

    using Base::Base;                        using Base::Base;
};                                       };

auto test()                              int main()
{                                        {
    auto d = Derived{ 5 };                   Derived d(1.2f);
}                                        }



我们甚至可以在最近的gcc 6.0版本上尝试,编译。 clang ++ 3.6,根据OP,MSVC14接受这个程序。

We can even try this on recent gcc 6.0 versions, and it still fails to compile. clang++3.6 and, according to the OP, MSVC14 accept this program.

这篇关于继承构造函数和大括号或初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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