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?
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:It should be possible to detect all anonymous
structs withinunions with regular expressions in theunion's definitions. But I do not see how regular expressions can be used to modify the list initialization appropriately.