This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 119a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.
2025-12-20
[Voted into WP at July, 2009 meeting.]
How does copy assignment for unions work? For example,
union U { int a; float b; }; void f() { union U u = { 5 }; union U v; v = u; // what happens here? }11.5 [class.union] is silent on the issue, therefore itseems that 11.4.5.3 [class.copy.ctor] applies. There is no specialcase for unions, thus paragraph 13 (memberwise assignment ofsubobjects) seems to apply. That would seem to imply these actions inthe compiler-generated copy assignment operator:
v.a = u.a; v.b = u.b;
And this is just wrong. For example, the lifetime ofv.a ends once the second assignment reuses the memoryofv.a.
We should probably prescribe “memcpy” copying forunions (both for the copy constructor and the assignment operator)unless the user provided his own special member function.
Proposed resolution (March, 2008):
Change 11.4.5.3 [class.copy.ctor] paragraph 8 as follows:
The implicitly-defined or explicitly-defaulted copy constructor fora non-union classX performs a memberwise copy of itssubobjects...
Add a new paragraph after 11.4.5.3 [class.copy.ctor] paragraph 8:
The implicitly-defined or explicitly-defaulted copy constructor for aunionX where all members have a trivial copy constructorcopies the object representation (6.9 [basic.types]) ofX. [Note: The behavior is undefined ifX isnot a trivial type. —end note]
Change 11.4.5.3 [class.copy.ctor] paragraph 13 as follows:
The implicitly-defined or explicitly-defaulted copy assignmentoperator fora non-union classX performs memberwiseassignment of its subobjects...
Add a new paragraph after 11.4.5.3 [class.copy.ctor] paragraph 13:
The implicitly-defined or explicitly-defaulted copy assignmentoperator for a unionX where all members have a trivial copyassignment operator copies the object representation (6.9 [basic.types]) ofX. [Note: The behavior is undefined ifX is not a trivial type. —end note]
Notes from the September, 2008 meeting:
The proposed wording needs to be updated to reflect thechanges adopted in papers N2757 and N2762, resolvingissue 683, which require “nonon-trivial” special member functions instead of “atrivial” function. Also, the notes regarding undefinedbehavior are incorrect, because the member functions involved aredefined as deleted when there are non-trivial members.
Proposed resolution (October, 2008):
Change 11.4.5.3 [class.copy.ctor] paragraph 8 as follows:
The implicitly-defined or explicitly-defaulted copy constructor fora non-union classX performs a memberwise copy of itssubobjects...
Add a new paragraph following 11.4.5.3 [class.copy.ctor] paragraph 8:
The implicitly-defined or explicitly-defaulted copy constructor for aunionX copies the object representation (6.9 [basic.types]) ofX.
Change 11.4.5.3 [class.copy.ctor] paragraph 13 as follows:
Add a new paragraph following 11.4.5.3 [class.copy.ctor] paragraph 13:
The implicitly-defined or explicitly-defaulted copy assignment operatorfor a unionX copies the object representation(6.9 [basic.types]) ofX.