Can you assign the value of one union member to another?

前端 未结 2 1513
没有蜡笔的小新
没有蜡笔的小新 2021-02-19 09:58

Consider the following code snippet:

union
{
   int a;
   float b;
};

a = /* ... */;

b = a;               // is this UB?
b = b + something;

I

2条回答
  •  独厮守ぢ
    2021-02-19 10:41

    Unfortunately I believe the answer to this question is that this operation on unions is under specified in C++, although self assignment is perfectly ok.

    Self assignment is well defined behavior, if we look at the draft C++ standard section 1.9 Program execution paragraph 15 has the following examples:

    void f(int, int);
    void g(int i, int* v) {
        i = v[i++]; // the behavior is undefined
        i = 7, i++, i++; // i becomes 9
    
        i = i++ + 1; // the behavior is undefined
        i = i + 1; // the value of i is incremented
    
        f(i = -1, i = -1); // the behavior is undefined
    }
    

    and self assignment is covered in the i = i + 1 example.

    The problem here is that unlike C89 forward which supports type-punning in C++ it is not clear. We only know that:

    In a union, at most one of the non-static data members can be active at any time

    but as this discussion in the WG21 UB study group mailing list shows this concept is not well understood, we have the following comments:

    While the standard uses the term "active field", it does not define it

    and points out this non-normative note:

    Note: In general, one must use explicit destructor calls and placement new operators to change the active member of a union. — end note

    so we have to wonder whether:

    b = a;
    

    makes b the active member or not? I don't know and I don't see a way to prove it with the any of the current versions of the draft standard.

    Although in all practicality most modern compilers for example gcc supports type-punning in C++, which means that the whole concept of the active member is bypassed.

提交回复
热议问题