Bug 68051 - copy constructor does not copy default initialized union member
Summary: copy constructor does not copy default initialized union member
Status: RESOLVED DUPLICATE of bug 66583
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-10-22 15:24 UTC by akju
Modified: 2015-10-23 06:48 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description akju 2015-10-22 15:24:36 UTC
The assertion at the end of the following code fails erroneously:

#include <string>
#include <cassert>

struct test
{
	union {int v = 0;};
	std::string dummy;
};

int main ()
{
	test x;
	x.v = 1;
	test y = x;
	assert (y.v == 1);
}

According to the assembly listing, it seems that the copy construction of y uses the default member initializer of v instead of x.v. This behaviour does not occur if the dummy member is removed causing the struct to be trivially copyable.
Comment 1 Markus Trippelsdorf 2015-10-22 16:59:10 UTC
This issue was fixed on trunk recently. The fix will hopefully be backported
to gcc-5.
Comment 2 Markus Trippelsdorf 2015-10-22 17:17:49 UTC
It is already fixed.

*** This bug has been marked as a duplicate of bug 66583 ***