An union with a const and a nonconst member?

后端 未结 3 1201
伪装坚强ぢ
伪装坚强ぢ 2021-01-01 12:40

This appears to be undefined behavior

union A {
  int const x;
  float y;
};

A a = { 0 };
a.y = 1;

The spec says

Cr

相关标签:
3条回答
  • 2021-01-01 13:11

    If it's any consolation - the Microsoft Xbox 360 compiler (which is based on Visual Studio's compiler) does error out. Which is funny, because that's usually the most lenient of the bunch.

    error C2220: warning treated as error - no 'object' file generated
    warning C4510: 'A' : default constructor could not be generated
        : see declaration of 'A'
    warning C4610: union 'A' can never be instantiated - user defined constructor required
    

    This error goes away if I take the const away. gcc-based compilers don't complain.

    EDIT: The Microsoft Visual C++ compiler has the same warning.

    0 讨论(0)
  • 2021-01-01 13:13

    It doesn't really make sense to have a const member of a union, and I'm surprised that the standard allows it. The purpose of all of the many limitations on what can go into a union is to arrive at a point where bitwise assignment will be a valid assignment operator for all members, and you can't use bitwise assignment to assign to a const int. My guess is that it's just a case that no one had previously thought of (although it affects C as well as C++, so it's been around for awhile).

    0 讨论(0)
  • 2021-01-01 13:15

    The latest C++0x draft standard is explicit about this:

    In a union, at most one of the non-static data members can be active at any time, that is, the value of at most one of the non-static data members can be stored in a union at any time.

    So your statement

    a.y = 1;
    

    is fine, because it changes the active member from x to y. If you subsequently referenced a.x as an rvalue, the behaviour would be undefined:

    cout << a.x << endl ; // Undefined!
    

    Your quote from the spec is not relevant here, because you are not creating any new object.

    0 讨论(0)
提交回复
热议问题