Talk:cpp/language/aggregate initialization

>> no user-provided constructors (explicitly defaulted or deleted constructors are allowed) Are deleted constructors really allowed? It doesn't work for me http://coliru.stacked-crooked.com/a/12f9a83938b94190 Balki (talk) 03:41, 18 April 2014 (PDT)
 * that's a GCC bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52707 clang works as intended: http://coliru.stacked-crooked.com/a/5efc1114524cb68d --Cubbi (talk) 09:10, 18 April 2014 (PDT)

>> I think it might be worthwhile to mention in the notes that flexible array members aren't valid constructs in C++. The two sentences (in serial) "aggregate initialization is recursive" + "If the object is an array of unknown size, and the supplied brace-enclosed initializer list has n clauses, the size of the array is n" may lead some fool (such as myself) to think otherwise.Nathan.s.chappell (talk) 04:52, 14 November 2018 (PST)

Got a few questions/opinions on the example section that I'd like to share:
I believe 2 more aggregate struct examples might be helpful(number 1 recently failed me when my struct contained an empty vector):

S s5 = {0}; //may fail to initialize internal aggregate (a[3])? S s6 = {}; //should fail as no default values are given(?)

Regarding: // char cr[3] = {'a', 'b', 'c', 'd'}; // too many initializer clauses

Is the following expression:

char cr[3] = {'a', 'b', 'c'} //char array containing no room for \0

1. Valid and fine 2. Valid but not recommended as it is not null-terminated 3. Valid C but invalid C++ 4. Other

And finally regarding: //   U u3 = { "asdf" }; // error: invalid conversion to int Should the comment read as "to int" or "from char*"?

My apologies if these questions are inappropriate.

--Skew (talk) 09:14, 21 October 2020 (PDT)
 * both {0} and {} fall under the paragraph "If the number of initializer clauses is less than the number of members or initializer list is completely empty". The char array with no room for \0 is mentioned in the last of the Notes (valid C invalid C++). Conversion is from const char[5] (type of "asdf" to int (the type of a), it fails because it's to int and not to const char* (the type of b). --Cubbi (talk) 06:25, 23 October 2020 (PDT)

I am very sad that designated initializers are restricted to being only in declared order. Omitting some fields and using the others in don't-care ordering was the entire reason this feature existed and was useful in C99, making this a complete misfeature that is utterly useless to add to the language in the present form. 124.157.79.144 15:27, 2 November 2020 (PST)
 * Designated initializes (as they are in C++20) can nonetheless be useful (at least) to:
 * emulate named actual parameters,
 * emulate function's default parameters skipping:

--Space Mission (talk) 13:18, 3 January 2021 (PST)
 * To be fair, C incompat is a common criticism of the change: a header shared between C and C++ still has to avoid this C feature. But it did standardize a common compiler extension that some C++ users found useful. --Cubbi (talk) 08:41, 4 January 2021 (PST)

Expand on brace elision
It's not clear where or why brace elision is allowed, think we should try to explain this. --Ybab321 (talk) 03:57, 27 May 2021 (PDT)

example of aggregate initialization inheritance would help to be complete
Is it possible to use designated initialization with inheritance?

Ron1010 (talk) 12:43, 6 February 2023 (PST)


 * Making your snipped runnable online, this is what we may use:

--Space Mission (talk) 13:35, 7 February 2023 (PST)