anonymous unions (C11) - Mailing list pgsql-hackers

From Peter Eisentraut
Subject anonymous unions (C11)
Date
Msg-id f00a9968-388e-4f8c-b5ef-5102e962d997@eisentraut.org
Whole thread Raw
Responses Re: anonymous unions (C11)
List pgsql-hackers
Here is another patch set to sprinkle some C11 features around the
code.  My aim is to make a little bit of use of several C11 features
as examples and encouragement for future code, and to test compilers
(although we're not yet at the point where this should really stress
any compiler).

Here, I'm proposing to make some use of anonymous unions, which are a
nice notational simplification if you nest unions inside structs,
which is pretty common in PostgreSQL code.

Often times, you have something like

struct important_thing
{
    int a;
    int b;
    union
    {
        int foo;
        int bar;
    } u;
}

(in PostgreSQL source code, often "u" is used, sometimes "d"), and
then you have to address foo as varname.u.foo, which often looks
cryptic (especially if there is more nesting).  With anonymous unions,
you can declare this as

struct important_thing
{
    int a;
    int b;
    union
    {
        int foo;
        int bar;
    };
}

and write varname.foo, which often seems clearer.

(C++ also allows this, so this will preserve C++ compatibility of the
headers.)

That said, I did go overboard here and converted all the struct/union
combinations I could find, but I'm not necessarily proposing to apply
all of them.  I'm proposing patches 0001 through 0004, which are
relatively simple or in areas that have already changed a few times
recently (so backpatching would not be trivial anyway), and/or they
are somewhat close to my heart because they originally motivated this
work a long time ago.  But if someone finds among the other patches
one that they particularly like, we could add that one as well.
Attachment

pgsql-hackers by date:

Previous
From: Bertrand Drouvot
Date:
Subject: Re: Add memory_limit_hits to pg_stat_replication_slots
Next
From: shveta malik
Date:
Subject: Re: Clear logical slot's 'synced' flag on promotion of standby