我尝试使用值初始化与值初始化的构造函数的成员(我不知道如果我真的使用良好的条件......)统一和值初始化
所以......当我定义:
struct A
{
int a_;
};
我能使用:
A a{5};
assert(m.a_==5);
但是,如果我想使用底架初始化和初始化列表构造
struct B
{
int b_ {1};
};
这并不编译(C++ 14:http://ideone.com/MQ1FMU):
B b{2};
以下是错误:
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' to 'const B&'
prog.cpp:10:8: note: constexpr B::B(B&&)
prog.cpp:10:8: note: no known conversion for argument 1 from 'int' to 'B&&'
有什么区别,概念明智? 非常感谢!
嗯,我认为这是因为'B'不是一个聚合,但它实际上似乎满足要求,据我所知。这简直是不平凡的。 – 2015-04-01 20:30:17
请注意,Ideone.com上的“C++ 14”是g ++ - 4.9.2,它不符合C++ 14标准(由此编译错误证明!) – Casey 2015-04-01 22:10:57