Search code examples
c++11default-constructorenum-class

c++11: enum member initialization


I have a class defined as follows

struct X {
    X() : data() {}

    int data;

    enum class Zzz : int { zero, one, two };
    Zzz  zzz;
};
...
X xval;

What is the value of xval.zzz - is undefined or X::Zzz.zero ? I know it will be undefined for regular enums and I am wondering whether typed enums behave differently.


Solution

  • It's uninitialised.

    Since the backing type is an int and that can contain a trap representation, the reading of xval.zzz prior to initialisation is undefined. (Out of interest, if the backing type was a char, unsigned char, or signed char, then the behaviour would be merely implementation defined.)