gpt4 book ai didi

c++ - 标准的哪一部分确定就地成员与初始化列表的优先级?

转载 作者:太空狗 更新时间:2023-10-29 20:08:14 25 4
gpt4 key购买 nike

我只是做了一个样例,从我个人的观点来看应该是不能编译的,或者至少要给出警告,但是Visual Studio 2017并没有给出任何警告。示例如下:

#include <stdexcept>

struct Foo {
Foo(int i) { throw std::runtime_error("Oh no:("); }
Foo(float f) {}
};

struct Bar {
Bar() {}
};

struct Baz {
Baz() : foo(5.0f) {}

Bar bar;
Foo foo = Foo(3);
Bar bar2;
};

int main()
{
Baz baz;
}

在我看来(但我不是语言律师),foo 的两个初始化(in-place vs initializer list)是模棱两可的。那么这种情况下的规则是什么?

最佳答案

明确首选构造函数中提供的值。

[class.base.init]/10: If a given non-static data member has both a default member initializer and a mem-initializer, the initialization specified by the mem-initializer is performed, and the non-static data member's default member initializer is ignored.

关于c++ - 标准的哪一部分确定就地成员与初始化列表的优先级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56062784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com