mercredi 1 mars 2017

C++11 empty list Initialization of a union - is it always guaranteed to initialize the full size of the union?

In C++11, I have the following union:

union SomeData
{
    std::uint8_t Byte;
    std::uint16_t Word;
    std::uint32_t DWord;
    unsigned char String[128];
}

If I initialize the union thusly;

SomeData data {};

Is the entire contents of the union guaranteed to be "zero'd" out? Put another way; is an empty union list-initializer functionally equivalent memset-ing the union to Zero?:

memset(&data, 0, sizeof(data));

In particular, I'm concerned about the string data. I'd like to ensure the entire length of the string contains zeros. It appears to work in my current compiler, but does the language of the spec guarantee this to always be true?

Aucun commentaire:

Enregistrer un commentaire