This is similar to POD structures containing a constant element , but it seems to be the other way around.
g ++ 4.8.3 compiles this code without errors and works correctly:
$ g++ -std=c++03 a.cpp -o a_gcc $ ./a_gcc 12
But clang ++ 3.5.1 creates an error (I manually wrapped the error message so that the code window would not scroll):
$ clang++ -std=c++03 a.cpp -o a_clang a.cpp:8:7: error: member function 'operator=' not viable: 'this' argument has type 'volatile A', but function is not marked volatile union U ^ a.cpp:3:8: note: 'operator=' declared here struct A ^ a.cpp:20:5: note: implicit copy assignment operator for 'U' first required here u2 = u1; ^ 1 error generated.
Does C ++ 03 allow a program to copy-assign a union containing mutable structures? I could not find anything in the C ++ 03 standard that defines the default copy constructor of a union.
I would like to know which compiler is correct, or if the standard is not clear at this point.
Edit: I found out that if I use the copy construct instead of the copy destination, then both clang ++ and g ++ will compile the program without errors. In particular, if I change main as follows:
int main() { U u1; u1.aa = 12; U u2 = u1; std::cout << u2.aa << std::endl; return 0; }
.. then it will work. I wonder why they are referred to in other words, clang ++.
source share