mercredi 1 avril 2015

Uniform and Value-initialization

I try to use value-initialization for members with value-initialization for constructors (I don't know if I really use the good terms...)


So... When I define:



struct A
{
int a_;
};


I'm able to use:



A a{5};
assert(m.a_==5);


However, if I want to use the member brace initializer AND an initialization-list constructor



struct B
{
int b_ {1};
};


This doesn't compile (c++14: http://ift.tt/1BNyaGx):



B b{2};


Here is the error:



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&&'


What is the difference, concept-wise? Many thanks!


Aucun commentaire:

Enregistrer un commentaire