This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Designated Initializers in C++
- From: David Brown <david at westcontrol dot com>
- To: Jakub Jelinek <jakub at redhat dot com>
- Cc: <gcc at gcc dot gnu dot org>
- Date: Tue, 16 Sep 2014 14:00:49 +0200
- Subject: Re: Designated Initializers in C++
- Authentication-results: sourceware.org; auth=none
- References: <54181B9B dot 7070507 at westcontrol dot com> <20140916112029 dot GY17454 at tucnak dot redhat dot com>
On 16/09/14 13:20, Jakub Jelinek wrote:
> On Tue, Sep 16, 2014 at 01:14:35PM +0200, David Brown wrote:
>> After a recent discussion about designated initializers in C++, I
>> noticed that they are accepted by modern gcc (when gcc extensions are
>> enabled).
>>
>> On <https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html>, the
>> documentation specifically says "This extension is not implemented in
>> GNU C++". That certainly used to be the case - as far as I have tested,
>> it was implemented in gcc 4.6 or 4.7.
>>
>> Could someone with commit access to the documentation fix that sentence?
>
> ??? Designated initializers definitely are not implemented in G++,
> what there is is just very limited parsing, but the C++ FE requires that
> the designators are just useless annotations, you can't initialize even
> a POD out of order with the designators, skip some field, initialize
> something twice etc.
>
> Jakub
>
The discussion I had been involved in was in the context of initialising
unions - I hadn't investigated further regarding designated initialisers
in structs.
My example code was this:
struct SE {
int x;
union {
int i;
float f;
double d;
};
};
struct SE a = { 0, {.f = 1 }};
struct SE b = { 1, {.d = 1 }};
struct SE c = { 2, {.i = 1 }};
Compiling and examining the object code gave identical results with C++
(--std=g++11) and C (--std=gnu11) with a recently built gcc 4.9.1 on
64-bit Linux. The code initialises the three structs/unions as desired.
With g++ and "-ansi -Wpedantic", I got a warning "ISO C++ does not allow
C99 designated initializers" on the three appropriate lines.
However, I have now tried a little with structs and designed
initialisers - and I see what you mean. I am getting the errors and
warnings you mention. So it works for unions - at least in the simple
case I tried - but not for structs.
I had thought I had found an undocumented feature - one that I know many
people would like to see in C++ (of preference, it should be in the
standards - but failing that as a gcc extension).
Thanks anyway,
David