Topic: C++0x: Dueling aggregate member initializers


Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Tue, 20 Oct 2009 23:08:48 CST
Raw View
On 20 Okt., 23:23, Scott Meyers <use...@aristeia.com> wrote:
> Consider:
>
>  struct S {
>    int x = 10;
>  };
>
>  S obj = { 5 };
>
> Is the value of obj.x 5 or 10?  I hope the answer is 5 (i.e., use the
> explicitly specified value instead of the default), but I can't find a
> place in draft C++0x where this issue is settled.  Can somebody please
> tell me what the correct answer is and where in draft C++0x I can find
> it?

I believe, that the standard does not give (yet) an exact answer to
that
question. IMO the most probable result would be that the program is
ill-formed, but that depends on the outcome of core issue

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#886

Let me argue why I think so:

First we note that the initialization that happens here belongs to
list-initialization. This clearly follows from [dcl.init]/16 [The
following
bases on N2960 numbering and wording], were we have a decision
chain starting with:

"The semantics of initializers are as follows. The destination type
is
the type of the object or reference being initialized and the source
type
is the type of the initializer expression. The source type is not
defined
when the initializer is a braced-init-list or when it is a
parenthesized list
of expressions.
    If the destination type is a reference type, see 8.5.3.
    If the destination type is an array of characters, an array of
char16_t,
an array of char32_t, or an array of wchar_t, and the initializer is a
string
literal, see 8.5.2.
    If the initializer is a braced-init-list, the object is list-
initialized (8.5.4).
[..]"

We fall into bullet three, because we have a /braced-init-list/ here.
This
leads us to 8.5.4 [dcl.init.list]/3:

"List-initialization of an object or reference of type T is defined as
follows:
    If T is an aggregate, aggregate initialization is performed (8.5.1).
[..]"

The central question is now: Is S an aggregate? Therefore we check
8.5.1 [dcl.init.aggr]:

"An aggregate is an array or a class (Clause 9) with no user-provided
constructors (12.1), no private or protected non-static data members
(Clause 11), no base classes (Clause 10), and no virtual functions
(10.3)."

Everything is clear except for the question whether this class type
has a user-provided constructor, because we use a special
initialization-
syntax here, that has the character of something which *looks* like
a constructor. The observation has lead to above mentioned issue.

a) *If* S shall be an aggregate, the standard does not yet clarify how
aggregate initialization in the presence of brace-or-equal-
initializers
works and we have an open question here. A reasonable outcome is
your expectation.

b) *If* S shall not be an aggregate (which is my current most
reasonable
estimate), then we must skip bullet 1 above. But proceeding with the
list of possible situations *none* of the following bullets apply,
except
the last one (ill-formed). Note that we have to check bullet 3 in a
swing-by
maneuver to find out that S has an implicitly declared default
constructor,
but that one doesn't match to an initializer list of the structure
{ 5 }.

HTH & 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: Scott Meyers <usenet@aristeia.com>
Date: Wed, 21 Oct 2009 11:58:16 CST
Raw View
Daniel Kr   gler wrote:
> I believe, that the standard does not give (yet) an exact answer to
> that
> question. IMO the most probable result would be that the program is
> ill-formed, but that depends on the outcome of core issue
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#886

Thanks for your insight on this question.

FWIW, I'm inclined to disagree with your assessment in issue 886 that
"in-class
member initializers are essentially syntactic sugar for mem-initializers,"
because I think of member initializers as *default* initializers -- the
initialization value to use if nothing else is provided.  As such, I
think that
aggregates should be allowed to have them.

Scott


--
[ 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: Scott Meyers <usenet@aristeia.com>
Date: Tue, 20 Oct 2009 15:23:34 CST
Raw View
Consider:

 struct S {
   int x = 10;
 };

 S obj = { 5 };

Is the value of obj.x 5 or 10?  I hope the answer is 5 (i.e., use the
explicitly specified value instead of the default), but I can't find a
place in draft C++0x where this issue is settled.  Can somebody please
tell me what the correct answer is and where in draft C++0x I can find
it?

Thanks,

Scott

--
[ 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                      ]