标准的哪一部分确定就地成员与初始化列表的优先级?
Which part of standard determines priority of in-place member vs initializer list?
我只是做了一个sample,从我个人的观点来看应该是编译不通过,至少给一个warning,但是Visual Studio2017没有给任何warning。示例如下:
#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.
我只是做了一个sample,从我个人的观点来看应该是编译不通过,至少给一个warning,但是Visual Studio2017没有给任何warning。示例如下:
#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.