https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86173
Bug ID: 86173 Summary: Default construction of a union (in std::optional) Product: gcc Version: 9.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: glisse at gcc dot gnu.org Target Milestone: --- Default construction of std::optional<A> always starts with a memset of the whole optional to 0, while it doesn't with clang using the same libstdc++. #include <optional> struct AA { double a[1024]; #ifndef TRIVIAL AA(); AA(AA const&); AA& operator=(AA const&); ~AA(); #endif }; typedef std::optional<AA> O; // O fff(){ return {}; } O fff(){ O o; return o; } The .original dump has *<retval> = {.D.34926={._M_payload={.D.34026={._M_empty={}}, ._M_engaged=0}}} which looks good, it says it is initializing the small _M_empty part of the union, but the gimple dump has *<retval> = {}; which eagerly zeroes everything.