lundi 4 décembre 2023

What are the rules for uniform initialization of a union [duplicate]

If i compile the following code ...

int main() 
{
    union U
    {
        struct { int i, j; } s;
        long long x, y;
        int z;
    };
    U li { 1 };
}

... either x or y becomes initialized with 1 from what I can deduce from the didsassembly. I noticed that when initializing a LARGE_INTERGER with Win32, where the QuadPart becomes initialized, which is very convenient for me.
But what are the rules for static initalization of unions ? Which part of the union is applied with the initialization value if there's no constructor ?
Edit: I've removed the language-lawyer tag someone applied here because I won't judge on that.

Aucun commentaire:

Enregistrer un commentaire