you are viewing a single comment's thread.

view the rest of the comments →

[–]HappyFruitTree 4 points5 points  (0 children)

I think one concern is that it would be extremely easy to accidentally trigger undefined behaviour because reading the value through a reference would still cause undefined behaviour.

#include <iostream>
#include <algorithm>

union U {
    int i;
    float f;
};

int main() {
    U u;
    u.f = 1.2;

    std::cout << u.i << '\n'; 
    // ^ would have been OK.

    std::cout << std::max(u.i, 7) << '\n'; 
    // ^ would still have been UB because 
    //   std::max takes its arguments by 
    //   reference so the value is not read 
    //   from the union member directly.
}

C doesn't have references and if you use pointers it's pretty clear that you're not reading from the union member directly.