2

I am porting legacy C++ code to work with GCC 9.2. Using C++20 and GNU extensions are valid options.

The legacy code makes heavy use of anonymous structs nested in unions and aggregate initialization with designated initializes for example:

union u 
{ 
    int a; 
    struct
    {
        int b;
        int c;
    }; 
};
u f = { .b = 1, .c = 2 };

This example does compile with clang -std=gnu++2a -Wall -Wextra, but it does not compile with g++ -std=gnu++2a -Wall -Wextra:

error: too many initializers for 'u'

As there are many cases where such constructs are applied in the code, it would be necessary to apply potential changes to the code in an automated way (for example with the help of regular expressions). How can I compile "this code" with GCC 9.2 by changing the code in an automated way and as little as possible?

2 Answers 2

2

By moving the nested structure to the first position within the union and initializing the structure like an non-anonymous structure compiles with g++ -std=gnu++2a -Wall -Wextra:

union u { 
    struct
    {
        int b;
        int c;
    }; 
    int a; 
};
u f = { {.b = 1, .c = 2 } };

It should be possible to detect all anonymous structs within unions with regular expressions in the union's definitions. But I do not see how regular expressions can be used to modify the list initialization appropriately.

0

You can initialize only one member of the union:

because of 15.6.2 [class.base.init] paragraph 8: An attempt to initialize more than one non-static data member of a union renders the program ill-formed.

so if you put: { .b=1, .c=2 } it sees you are initializing two members.

Then you can initialize either, .b or .c but not both.

Moreover:

If the element is an anonymous union object and the initializer list is a designated-initializer-list, the anonymous union object is initialized by the designated-initializer-list​​{​​D​​}​, where ​D​ is the ​designated-initializer-clause​ naming a member of the anonymous union object. There shall be only one such designated-initializer-clause​.

The key is: where ​D​ is the ​designated-initializer-clause​ naming a member of the anonymous union object

You may get more info here.

To be able to give a value to the members of the struct you should give an element:

union u 
{ 
    int a; 
    struct
    {
        int b;
        int c;
    } v; // <-- this
};

u f = { .v = { .b = 1, .c = 2 } };

But this would break the accesses to every u: i.e. f.v.b = 0.

So I think your construct is not compatible with C++20. Why clang compiles and works? May be it is an extension when there are no constructors and/or destructors involved.

2
  • Correct, the struct definition in the OP / question is not compatible with C++20 because of the anonymous structure. Anonymous structures are supported by g++ as a GNU extension. Unfortunately, as it seems, not in combination with designated initializers? The designators can only be used within the inner structure and can not be used directly from the enclosing aggregator (union in this case). I hope I overlooked something and it actually does work with g++. Jul 4, 2020 at 6:41
  • Discussion of clang's support for designated initializers: stackoverflow.com/questions/54459635/…
    – muusbolla
    Nov 6, 2020 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.