Topic: Brace initialisation and members with initialisers
Author: Yechezkel Mett <ymett.on.usenet@gmail.com>
Date: Mon, 4 May 2009 22:52:20 CST Raw View
As far as I can tell from the current draft (N2857), initialising an
aggregate with braces will ignore any member initialisers in the class
definition.
Given
struct C
{
int a = 1;
int b = 2;
};
C c1; // c1.a is 1, c1.b is 2
C c2{}; // c2.a is 0, c2.b is 0 !
C c3 = C(); // c3.a is 1, c3.b is 2
C c4 = C{}; // c4.a is 0, c4.b is 0 !
This seems inconsistent and likely to cause confusion.
It would seem better to change 8.5.1p7 from
If there are fewer initializer-clauses in the list than there are
members in the aggregate, then each member not explicitly initialized
shall be value-initialized (8.5).
to
If there are fewer initializer-clauses in the list than there are
members in the aggregate, then for each member not explicitly
initialized
? if it is a non-static data member that has a brace-or-equal-
initializer, it is initialized as specified in 8.5;
? otherwise, it is value-initialized (8.5).
Yechezkel Mett
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: daniel.kruegler@googlemail.com
Date: Tue, 5 May 2009 14:16:35 CST Raw View
On May 5, 6:52 am, Yechezkel Mett <ymett.on.use...@gmail.com> wrote:
> As far as I can tell from the current draft (N2857), initialising an
> aggregate with braces will ignore any member initialisers in the class
> definition.
>
> Given
>
> struct C
> {
> int a = 1;
> int b = 2;
>
> };
>
> C c1; // c1.a is 1, c1.b is 2
> C c2{}; // c2.a is 0, c2.b is 0 !
> C c3 = C(); // c3.a is 1, c3.b is 2
> C c4 = C{}; // c4.a is 0, c4.b is 0 !
>
> This seems inconsistent and likely to cause confusion.
>
> It would seem better to change 8.5.1p7 from
>
> If there are fewer initializer-clauses in the list than there are
> members in the aggregate, then each member not explicitly initialized
> shall be value-initialized (8.5).
>
> to
>
> If there are fewer initializer-clauses in the list than there are
> members in the aggregate, then for each member not explicitly
> initialized
> ? if it is a non-static data member that has a brace-or-equal-
> initializer, it is initialized as specified in 8.5;
> ? otherwise, it is value-initialized (8.5).
I agree that you are pointing to core issue. An alternative
fix would be to exclude a class type from being an
aggregate if it contains any direct brace-or-equal-initializer.
Greetings from Bremen,
Daniel Kr gler
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: wasti.redl@gmx.net
Date: Fri, 8 May 2009 19:19:30 CST Raw View
On May 5, 10:16 pm, daniel.krueg...@googlemail.com wrote:
> On May 5, 6:52 am, Yechezkel Mett <ymett.on.use...@gmail.com> wrote:
>
>
>
> > As far as I can tell from the current draft (N2857), initialising an
> > aggregate with braces will ignore any member initialisers in the class
> > definition.
>
> > Given
>
> > struct C
> > {
> > int a = 1;
> > int b = 2;
>
> > };
>
> > C c1; // c1.a is 1, c1.b is 2
> > C c2{}; // c2.a is 0, c2.b is 0 !
> > C c3 = C(); // c3.a is 1, c3.b is 2
> > C c4 = C{}; // c4.a is 0, c4.b is 0 !
>
> > This seems inconsistent and likely to cause confusion.
>
> > It would seem better to change 8.5.1p7 from
>
> > If there are fewer initializer-clauses in the list than there are
> > members in the aggregate, then each member not explicitly initialized
> > shall be value-initialized (8.5).
>
> > to
>
> > If there are fewer initializer-clauses in the list than there are
> > members in the aggregate, then for each member not explicitly
> > initialized
> > ? if it is a non-static data member that has a brace-or-equal-
> > initializer, it is initialized as specified in 8.5;
> > ? otherwise, it is value-initialized (8.5).
>
> I agree that you are pointing to core issue. An alternative
> fix would be to exclude a class type from being an
> aggregate if it contains any direct brace-or-equal-initializer.
>
That should even be implicit. brace-or-equal-initializers are,
semantically speaking, default values for missing mem-initializers in
the mem-initializer-seq. Thus, any class with a brace-or-equal-
initializer has what amounts to a user-defined constructor.
Unfortunately, I don't think the wording supports my interpretation.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: daniel.kruegler@googlemail.com
Date: Sat, 9 May 2009 23:22:46 CST Raw View
On 9 Mai, 03:19, wasti.r...@gmx.net wrote:
> On May 5, 10:16 pm, daniel.krueg...@googlemail.com wrote:
>
> That should even be implicit. brace-or-equal-initializers are,
> semantically speaking, default values for missing mem-initializers in
> the mem-initializer-seq. Thus, any class with a brace-or-equal-
> initializer has what amounts to a user-defined constructor.
>
> Unfortunately, I don't think the wording supports my interpretation.
Exactly this is the reason why we have an issue here:
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#886
Here are some relevant quotes:
1) 12.1 [class.ctor]/5:
"[..]. If there is no user-declared constructor for class X,
a constructor having no parameters is implicitly declared.
An implicitly-declared default constructor is an inline
public member of its class. A default constructor is trivial
if it is not user-provided (8.4) and if:
[..]
no non-static data member of its class has a
brace-or-equal-initializer, and [..] "
2) 8.4 [dcl.fct.def]/9:
"[..] A special member function is user-provided if it is
user-declared and not explicitly defaulted on its first
declaration.[..]"
3) 8.5.1 [dcl.init.aggr]:
"An aggregate is an array or a class (Clause 9) with no
user-provided constructors (12.1),[..]"
So as currently written YM's class is an aggregate with
no user-provided constructor, which seems rather odd.
Greetings from Bremen,
Daniel Kr gler
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]