the c++ standard not allow "= initializer-clause" form of brace-or-equal-initializer (see [dcl.init]) in mem-initializer (see [class.base.init]). example:
struct foo { int x; foo(int y): x = y { } };
gives compile-time error.
why that? there syntactic ambiguity if such construct allowed? if not, or there proposals add feature language?
update. semantics expect of such initializer in context same of in context of usual variable initialization. in opinion, make initialization syntax of language more consistent.
update 2. pointed out nathanoliver, if initialization of member not depend on selected constructor and/or constructor parameters, member can initialized through default member initializer (see [class.mem]). if does, leaves using direct or list initialization.
like others have said, it's simple not allowed syntax in ctor initialization.
note that, non-primitive classes, ctor() : xyz(...)
form of initialization ctor call on type of member xyz
. while ctor(): xyz{...}
aggregate initialization of either array/list or member initialization of structs/unions have no ctors.
a ctor() : xyz = something
seem have 2 things: 1) default ctor
call , 2) call operator=
type of member xyz
of double initialization. why disallowed. of course, non-primitive cases, guess reduced copy-ctor
call eliminate double initialization.
now, maybe convince allowing primitive types only ok, introduce other complications language , compilers.
Comments
Post a Comment