我嘗試使用值初始化與值初始化的構造函數的成員(我不知道如果我真的使用良好的條件......)統一和值初始化
所以......當我定義:
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