https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102942
Bug ID: 102942 Summary: Implicit copy constructor not elided Product: gcc Version: 11.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: other Assignee: unassigned at gcc dot gnu.org Reporter: albin at yahoo dot com Target Milestone: --- This is seen on every GCC version. The copy-constructor for a class with a data buffer only gets elided if the copy-constructor is user provided. So if I have a copy-constructor that explicitly calls memcpy, the compiler will elide it when it can (and thus NOT call memcpy), but if I do not declare any such constructor, the compiler *will* call mempcy even if the call could be elided. https://godbolt.org/z/5YvWTE847 #include <cstring> struct StructWithImplicitCpyCtr { StructWithImplicitCpyCtr(); float data[5000]; }; struct StructWithExplicitMemCpyCpyCtr { StructWithExplicitMemCpyCpyCtr(); StructWithExplicitMemCpyCpyCtr(StructWithExplicitMemCpyCpyCtr const& other) { std::memcpy(data, other.data, sizeof(data)); } float data[5000]; }; void take(StructWithImplicitCpyCtr); void take(StructWithExplicitMemCpyCpyCtr); void foobar() { // The copy constructor explicitly calls memcpy, but copy elision removes it take(StructWithExplicitMemCpyCpyCtr()); // Here there is no explicit copy constructor to elide, we should expect to see // similar code generated, instead we see a call to memcpy!!! take(StructWithImplicitCpyCtr()); }