Designated Initializer Lists

87 views
Skip to first unread message

rob...@chromium.org

unread,
Jun 21, 2018, 6:46:14 PM6/21/18
to cxx
Since the switch to clang, I've seen some use of C99/C++20 Designated Initializer Lists start appearing.

Examples:

Particularly fun is that some C99 expressions are not allowed in C++20:
Change:​ In C++, designated initialization support is restricted compared to the corresponding functionality in C. In C++, designators for non-static data members must be specified in declaration order, designators for array elements and nested designators are not supported, and designated and non-designated initializers cannot be mixed in the same initializer list.
Example:
struct A { int x, y; };
struct B { struct A a; };
struct A a = {.y = 1, .x = 2}; // valid C, invalid C++ 
int arr[3] = {[1] = 5}; // valid C, invalid C++ 
struct B b = {.a.x = 0}; // valid C, invalid C++ 
struct A a = {.x = 1, 2}; // valid C, invalid C++ 

Given that this doesn't seem to yet be finalized for C++20 and it isn't quite ready on Clang, do we want to ban such usage in Chromium until it is fully defined?

Peter Kasting

unread,
Jun 21, 2018, 7:01:22 PM6/21/18
to Robert Liao, cxx
It should already be banned AFAIK, since it is not in the C++11/14 approved language features list, and nothing past those is supposed to be supported.

I'm surprised we compile in a mode where clang allows this.

PK

Nico Weber

unread,
Jun 21, 2018, 7:11:37 PM6/21/18
to Peter Kasting, Robert Liao, cxx
clang allows C99 features in C++ mode as an extension. We could pass -Wc99-extensions to flag those as warnings and not accept them, but it seems fine to me to allow them if people think they're useful. (If others disagree, I don't feel super strongly though.)

If C++20 makes some C99 things invalid, I'm sure there will come a warning to flag the incompatible bits soon, so maybe we should wait for that to happen and then turn on only that. Given that we're still working on the switch to 17, and 20 is still years away from being ratified, I'm not sure that's a huge cause for concern.

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAAHOzFCS%2BnNKcAvyN0gcjvgy0iHn1mB2M6FiMgoFtfGf%3D%3DYUAA%40mail.gmail.com.

Peter Kasting

unread,
Jun 21, 2018, 7:18:14 PM6/21/18
to Nico Weber, Robert Liao, cxx
On Thu, Jun 21, 2018 at 4:11 PM Nico Weber <tha...@chromium.org> wrote:
clang allows C99 features in C++ mode as an extension. We could pass -Wc99-extensions to flag those as warnings and not accept them, but it seems fine to me to allow them if people think they're useful. (If others disagree, I don't feel super strongly though.)

Does that warn only for things that are "valid C99 but not valid C++"?

If so, I'd vote for turning on that warning.   The C++ standards committee thinks about the question of how to maintain compatibility with C over time, so until they allow something into C++ itself, I would defer to their judgment.

PK

Nico Weber

unread,
Jun 21, 2018, 7:22:35 PM6/21/18
to Peter Kasting, Robert Liao, cxx
On Thu, Jun 21, 2018 at 7:18 PM Peter Kasting <pkas...@google.com> wrote:
On Thu, Jun 21, 2018 at 4:11 PM Nico Weber <tha...@chromium.org> wrote:
clang allows C99 features in C++ mode as an extension. We could pass -Wc99-extensions to flag those as warnings and not accept them, but it seems fine to me to allow them if people think they're useful. (If others disagree, I don't feel super strongly though.)

Does that warn only for things that are "valid C99 but not valid C++"?

I think so, but I haven't tried. I only looked up the designated init code in clang in response to this thread and saw that this warning exists. You can try sending a tryjob with it set and checking what it finds :-) (disable warnings as errors to see more than just the first TU.)

Peter Kasting

unread,
Jun 21, 2018, 7:34:00 PM6/21/18
to Nico Weber, Robert Liao, cxx
On Thu, Jun 21, 2018 at 4:22 PM Nico Weber <tha...@chromium.org> wrote:
You can try sending a tryjob with it set and checking what it finds :-) (disable warnings as errors to see more than just the first TU.)

Sadly I can't spare bandwidth for this right now -- but I filed https://bugs.chromium.org/p/chromium/issues/detail?id=855271 in case someone else would like to.

PK 

Sébastien Marchand

unread,
Apr 24, 2019, 7:28:38 PM4/24/19
to cxx, tha...@chromium.org, rob...@chromium.org
(resurrecting an old thread)

It looks like we never came up with a decision on this? There's at least 30 places where designated initializers are being used in the codebase (https://cs.chromium.org/search/?q=%5C%7B%5Cs*%5C.%5Cw%2B%5Cs*%3D+-f:third_party&sq=package:chromium&type=cs) and I'm guilty for a few of those . If you think that these should be removed then I can remove my use cases and open a crbug to get the other one removed (IMO it's probably not worth the trouble but I'm not an expert on this topic :) ).

On Thursday, June 21, 2018 at 7:34:00 PM UTC-4, Peter Kasting wrote:

Peter Kasting

unread,
Apr 24, 2019, 8:06:16 PM4/24/19
to Sébastien Marchand, cxx, Nico Weber, Robert Liao
On Wed, Apr 24, 2019 at 4:28 PM Sébastien Marchand <sebma...@chromium.org> wrote:
(resurrecting an old thread)

It looks like we never came up with a decision on this? There's at least 30 places where designated initializers are being used in the codebase (https://cs.chromium.org/search/?q=%5C%7B%5Cs*%5C.%5Cw%2B%5Cs*%3D+-f:third_party&sq=package:chromium&type=cs) and I'm guilty for a few of those . If you think that these should be removed then I can remove my use cases and open a crbug to get the other one removed (IMO it's probably not worth the trouble but I'm not an expert on this topic :) ).

The Google styleguide forbids nonstandard extensions, explicitly mentioning designated initializers as an example.  See http://google.github.io/styleguide/cppguide.html#Nonstandard_Extensions .

Given this, I think we should remove these.

PK

Robert Liao

unread,
Apr 24, 2019, 8:57:36 PM4/24/19
to Peter Kasting, Sébastien Marchand, cxx, Nico Weber
+1 to that. 

-wc99-extensions warns on a few other features that we do use in Chrome. It wasn't possible to just single out designated initializers.
I didn't have the bandwidth to add this support to clang and to fix up Chrome, so I let things stand as we waited for MSVC to catch up.

Peter Kasting

unread,
Apr 24, 2019, 9:31:41 PM4/24/19
to Robert Liao, Sébastien Marchand, cxx, Nico Weber
On Wed, Apr 24, 2019 at 5:57 PM Robert Liao <rob...@chromium.org> wrote:
+1 to that. 

-wc99-extensions warns on a few other features that we do use in Chrome. It wasn't possible to just single out designated initializers.
I didn't have the bandwidth to add this support to clang and to fix up Chrome, so I let things stand as we waited for MSVC to catch up.

FWIW, the style guide prohibition on extensions is broad and covers more things, likely including those "features we do use in Chrome", so I would vote to remove those too :)

PK 

Jeremy Roman

unread,
Apr 25, 2019, 9:53:20 AM4/25/19
to Peter Kasting, Robert Liao, Sébastien Marchand, cxx, Nico Weber
+1, with the obvious exception that such things are often useful if contained to limited areas and protected by macros (__attribute__ and __declspec being the most obvious, but not only, examples).

PK 

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.

Sébastien Marchand

unread,
Apr 25, 2019, 10:10:02 AM4/25/19
to Jeremy Roman, Peter Kasting, Robert Liao, cxx, Nico Weber
Sounds good to me! Thanks for the clarifications on this, I'll remove my use cases and open a crbug to get the other ones removed (I'll try to cc all the persons who owns the parts of the code using this).
--
Sébastien Marchand | Software Developer | sebma...@google.com 


Sébastien Marchand

unread,
Apr 25, 2019, 11:54:09 AM4/25/19
to Jeremy Roman, Peter Kasting, Robert Liao, cxx, Nico Weber
FYI I've opened crbug.com/956581 to track this.

Sébastien Marchand

unread,
Dec 12, 2019, 1:12:54 PM12/12/19
to Jeremy Roman, Peter Kasting, Robert Liao, cxx, Nico Weber
The internal style guide now allows designated initializers ( go/cstyle#Designated_initializers), they are using the "-Wreorder-init-list" flag to prevent potentially bad use cases.

Should we reconsider supporting this feature or should we wait for the public style guide to be updated?  

Peter Kasting

unread,
Dec 12, 2019, 1:54:18 PM12/12/19
to Sébastien Marchand, Jeremy Roman, Robert Liao, cxx, Nico Weber
On Thu, Dec 12, 2019 at 10:12 AM Sébastien Marchand <sebma...@chromium.org> wrote:
The internal style guide now allows designated initializers ( go/cstyle#Designated_initializers), they are using the "-Wreorder-init-list" flag to prevent potentially bad use cases.

Should we reconsider supporting this feature or should we wait for the public style guide to be updated?

Do these even compile with --std=c++14 and the other flags we pass?  That is, is the extension to support these on by default?

(Personally, I'd prefer to wait until Chromium is actually C++20 to support these; I'm uncomfortable using nonstandard extensions unless we have to.)

PK 

Sébastien Marchand

unread,
Dec 12, 2019, 2:37:48 PM12/12/19
to Peter Kasting, Jeremy Roman, Robert Liao, cxx, Nico Weber
On Thu, Dec 12, 2019 at 1:54 PM Peter Kasting <pkas...@google.com> wrote:
On Thu, Dec 12, 2019 at 10:12 AM Sébastien Marchand <sebma...@chromium.org> wrote:
The internal style guide now allows designated initializers ( go/cstyle#Designated_initializers), they are using the "-Wreorder-init-list" flag to prevent potentially bad use cases.

Should we reconsider supporting this feature or should we wait for the public style guide to be updated?

Do these even compile with --std=c++14 and the other flags we pass?  That is, is the extension to support these on by default?

I don't know, using designated initializers in //src seems to work fine but I don't see the "--std=c++14" flag on the compile command line...

I don't have a strong opinion on this. Designated initializer lists are convenient and can sometimes save us from using some quite verbose builder patterns but if people are not comfortable using them for now then it's fine, we can wait. I mostly wanted to mention that this is not allowed in the Google Style guide.
 

(Personally, I'd prefer to wait until Chromium is actually C++20 to support these; I'm uncomfortable using nonstandard extensions unless we have to.)

PK 

Peter Kasting

unread,
Dec 12, 2019, 3:47:37 PM12/12/19
to Sébastien Marchand, Jeremy Roman, Robert Liao, cxx, Nico Weber
On Thu, Dec 12, 2019 at 11:37 AM Sébastien Marchand <sebma...@chromium.org> wrote:
On Thu, Dec 12, 2019 at 1:54 PM Peter Kasting <pkas...@google.com> wrote:
On Thu, Dec 12, 2019 at 10:12 AM Sébastien Marchand <sebma...@chromium.org> wrote:
The internal style guide now allows designated initializers ( go/cstyle#Designated_initializers), they are using the "-Wreorder-init-list" flag to prevent potentially bad use cases.

Should we reconsider supporting this feature or should we wait for the public style guide to be updated?

Do these even compile with --std=c++14 and the other flags we pass?  That is, is the extension to support these on by default?

I don't know, using designated initializers in //src seems to work fine but I don't see the "--std=c++14" flag on the compile command line...

From chatting with Rob earlier it sounds like we support these right now due to compiling with C99 extensions enabled, which means not only are there some in our codebase already, but people may add ones that violate the C++20 rules.

MSVC never supported C99, so any uses here must have been added since we went clang-only.  My preference would be to try and remove any use of C99 extensions, disable C99 extensions in our build globally, and then wait for C++20.  But maybe that isn't the right cost/benefit decision.

PK 

Scott Graham

unread,
Dec 12, 2019, 3:59:51 PM12/12/19
to Peter Kasting, Sébastien Marchand, Jeremy Roman, Robert Liao, cxx, Nico Weber
MSVC >= 2015 supported the majority of C99 (including designated initializers) so I don't think there's any problem in practice.
 

PK 

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.

Robert Liao

unread,
Dec 12, 2019, 4:15:22 PM12/12/19
to Scott Graham, Peter Kasting, Sébastien Marchand, Jeremy Roman, cxx, Nico Weber
While MSVC did have some C99 support, it didn't support the designated initializers. Chrome didn't start getting them until the full switch to clang. (I used to do a parallel MSVC build, finding these new uses).

Sébastien Marchand

unread,
Dec 12, 2019, 5:08:06 PM12/12/19
to Robert Liao, Scott Graham, Peter Kasting, Jeremy Roman, cxx, Nico Weber
On Thu, Dec 12, 2019 at 4:15 PM Robert Liao <rob...@chromium.org> wrote:
While MSVC did have some C99 support, it didn't support the designated initializers. Chrome didn't start getting them until the full switch to clang. (I used to do a parallel MSVC build, finding these new uses).

On Thu, Dec 12, 2019 at 12:59 PM Scott Graham <sco...@chromium.org> wrote:


On Thu, Dec 12, 2019 at 12:47 PM 'Peter Kasting' via cxx <c...@chromium.org> wrote:
On Thu, Dec 12, 2019 at 11:37 AM Sébastien Marchand <sebma...@chromium.org> wrote:
On Thu, Dec 12, 2019 at 1:54 PM Peter Kasting <pkas...@google.com> wrote:
On Thu, Dec 12, 2019 at 10:12 AM Sébastien Marchand <sebma...@chromium.org> wrote:
The internal style guide now allows designated initializers ( go/cstyle#Designated_initializers), they are using the "-Wreorder-init-list" flag to prevent potentially bad use cases.

Should we reconsider supporting this feature or should we wait for the public style guide to be updated?

Do these even compile with --std=c++14 and the other flags we pass?  That is, is the extension to support these on by default?

I don't know, using designated initializers in //src seems to work fine but I don't see the "--std=c++14" flag on the compile command line...

From chatting with Rob earlier it sounds like we support these right now due to compiling with C99 extensions enabled, which means not only are there some in our codebase already, but people may add ones that violate the C++20 rules.

MSVC never supported C99, so any uses here must have been added since we went clang-only.  My preference would be to try and remove any use of C99 extensions, disable C99 extensions in our build globally, and then wait for C++20.  But maybe that isn't the right cost/benefit decision.

I've opened crbug.com/956581 to track the removal of these use cases but never found the energy to push on this (I've fixed my use cases).
 

MSVC >= 2015 supported the majority of C99 (including designated initializers) so I don't think there's any problem in practice.
 

PK 

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAAHOzFB7ULn781-oTOzAGPJzKFHAL%2By%2BOFPLBoprvigDgSHAHA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages