Designated initializers (like C99)

228 views
Skip to first unread message

Daryle Walker

unread,
Oct 25, 2013, 5:56:35 PM10/25/13
to std-pr...@isocpp.org
Should we add designated initializer syntax, like C-1999 has? Is there any fundamental breakage/hatred to the idea even if it's officially proposed, like C-1999's variable-length arrays?

Glancing at the feature from a copy of the C-1999 Standard, I'm wondering about a case of how it works with unions. You can specify a non-first member to initialize a union. There is also a rule that if a sub-object is covered more than once then the lexically-last one takes precedence. What about:

MyType whatever = { .variantA = 6, .variantB = 'c' };

where the two members are union siblings. Here, I think the second one takes precedence, so the owning union is initialized via its "variantB" member, and it's set to 'c'.

MyType whatever = { .something.variantA.x = t, .something.variantB.y = 1L };

I expect the same thing to happen. The "something" union member is set to use "variantB" as its current member and anything from "variantA" is ignored. The "y" sub-member is initialized to 1, and any other sub-members are value-initialized (assuming "variantB" is non-union).

An alternate opinion: when a union's active member is changed via a sub-member's initialization, the result should be ill-formed?

MyType whatever = { .s.AA.x = 1, .s.BB[4] = L'x', .s.AA.y = hello };

As before, "AA" and "BB" are union member siblings. At the end of the initialization, the "AA" member of the "s" union is initialized. The "y" sub-member is set to an enumeration, but does the "x" sub-member join the other sub-members in being value-initialized, or is the first initialization clause reinstated (although it was voided by the second)?

Hopefully, someone in C's version of the working group committees has already asked this, since it doesn't seem to be covered in C99. (Maybe C11 already has a resolution.)

Daryle W.

Richard Smith

unread,
Oct 25, 2013, 6:06:18 PM10/25/13
to std-pr...@isocpp.org
On Fri, Oct 25, 2013 at 2:56 PM, Daryle Walker <dar...@gmail.com> wrote:
Should we add designated initializer syntax, like C-1999 has? Is there any fundamental breakage/hatred to the idea even if it's officially proposed, like C-1999's variable-length arrays?

This has been suggested on the C++ ext reflector a couple of times, and has found opposition and very little support.
 
Glancing at the feature from a copy of the C-1999 Standard, I'm wondering about a case of how it works with unions. You can specify a non-first member to initialize a union. There is also a rule that if a sub-object is covered more than once then the lexically-last one takes precedence. What about:

MyType whatever = { .variantA = 6, .variantB = 'c' };

where the two members are union siblings. Here, I think the second one takes precedence, so the owning union is initialized via its "variantB" member, and it's set to 'c'.

MyType whatever = { .something.variantA.x = t, .something.variantB.y = 1L };

I expect the same thing to happen. The "something" union member is set to use "variantB" as its current member and anything from "variantA" is ignored. The "y" sub-member is initialized to 1, and any other sub-members are value-initialized (assuming "variantB" is non-union).

An alternate opinion: when a union's active member is changed via a sub-member's initialization, the result should be ill-formed?

MyType whatever = { .s.AA.x = 1, .s.BB[4] = L'x', .s.AA.y = hello };

As before, "AA" and "BB" are union member siblings. At the end of the initialization, the "AA" member of the "s" union is initialized. The "y" sub-member is set to an enumeration, but does the "x" sub-member join the other sub-members in being value-initialized, or is the first initialization clause reinstated (although it was voided by the second)?

Hopefully, someone in C's version of the working group committees has already asked this, since it doesn't seem to be covered in C99. (Maybe C11 already has a resolution.)

IIRC C has a DR on this.

Sean Middleditch

unread,
Oct 27, 2013, 2:11:34 AM10/27/13
to std-pr...@isocpp.org
On Friday, October 25, 2013 3:06:18 PM UTC-7, Richard Smith wrote:
On Fri, Oct 25, 2013 at 2:56 PM, Daryle Walker <dar...@gmail.com> wrote:
Should we add designated initializer syntax, like C-1999 has? Is there any fundamental breakage/hatred to the idea even if it's officially proposed, like C-1999's variable-length arrays?

This has been suggested on the C++ ext reflector a couple of times, and has found opposition and very little support.

This feature (or a more limited subset of it at least) was brought up "the answer" to named parameters in that thread recently.  If you recall, is the opposition to the idea of named initializers entirely or to the C99 syntax/semantics in particular?

Daryle Walker

unread,
Oct 27, 2013, 8:54:34 PM10/27/13
to std-pr...@isocpp.org
I took a quick glance at ISO's WG 14 page (the C group, like ours is WG 21), and I didn't see it. (None of the titles suggested anything about the designation feature.) Their group is really small, like the issues and proposals counts are orders of magnitude smaller! Was I looking at the wrong page?

Or maybe no DR mentioned it because it was resolved in C11. Does anyone here have a copy?

Daryle W.

Richard Smith

unread,
Oct 28, 2013, 2:15:54 PM10/28/13
to std-pr...@isocpp.org
The closest I can find is:


But that doesn't cover this case. The discussion I was thinking of was this:

 
Or maybe no DR mentioned it because it was resolved in C11. Does anyone here have a copy?

Yes, and C11 seems to have the same wording hole. 
Reply all
Reply to author
Forward
0 new messages