uuu777
uuu777

Reputation: 901

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.

Upvotes: 4

Views: 2352

Answers (1)

Bathsheba
Bathsheba

Reputation: 234665

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.)

Upvotes: 4

Related Questions