我尝试对构造函数的值初始化的成员使用值初始化(我不知道我是否真的使用了良好的术语......)
所以......当我定义:
struct A
{
int a_;
};
Run Code Online (Sandbox Code Playgroud)
我可以使用:
A a{5};
assert(m.a_==5);
Run Code Online (Sandbox Code Playgroud)
但是,如果我想使用member brace initializer和一个初始化列表构造函数
struct B
{
int b_ {1};
};
Run Code Online (Sandbox Code Playgroud)
这不编译(c ++ 14:http://ideone.com/MQ1FMU):
B b{2};
Run Code Online (Sandbox Code Playgroud)
这是错误:
prog.cpp:19:7: error: no matching function for call to 'B::B(<brace-enclosed initializer list>)'
B b{2};
^
prog.cpp:19:7: note: candidates are:
prog.cpp:10:8: note: constexpr B::B()
struct B
^
prog.cpp:10:8: note: candidate expects 0 arguments, 1 provided
prog.cpp:10:8: note: constexpr B::B(const B&)
prog.cpp:10:8: note: no known conversion for argument 1 from 'int' …Run Code Online (Sandbox Code Playgroud)