public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug other/102942] New: Implicit copy constructor not elided
@ 2021-10-26  8:49 albin at yahoo dot com
  2021-10-28  9:35 ` [Bug c++/102942] " redi at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: albin at yahoo dot com @ 2021-10-26  8:49 UTC (permalink / raw)
  To: gcc-bugs

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());
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-10-28 14:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-26  8:49 [Bug other/102942] New: Implicit copy constructor not elided albin at yahoo dot com
2021-10-28  9:35 ` [Bug c++/102942] " redi at gcc dot gnu.org
2021-10-28  9:37 ` redi at gcc dot gnu.org
2021-10-28  9:57 ` redi at gcc dot gnu.org
2021-10-28 14:05 ` matthijsvanduin at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).