https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117046

            Bug ID: 117046
           Summary: -Wclass-memaccess provides misleading diagnostics on
                    std::memcpy
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: carlosgalvezp at gmail dot com
  Target Milestone: ---

Consider this example:

#include <array>
#include <cstring>

struct Foo
{
    int a{123};
};

void f()
{
    Foo dst{};
    std::array<unsigned char, 4> bytes{};
    std::memcpy(&dst, &bytes, sizeof(dst));        // NOK
    std::memcpy(&dst, bytes.data(), sizeof(dst));  // OK
}

https://godbolt.org/z/6hEfa9cd5

Here, GCC complains with following diagnostic:

copying an object of non-trivial type 'struct Foo' ...

It sounds therefore like the problem is that "Foo" is non-trivial. But that's
not actually the problem; std::memcpy accepts non-trivial types, as long as
they are trivially copyable. This is clear from the line below.

The problem was in passing &bytes instead of bytes.data().

Would it be possible to improve the diagnostic?

Thanks!

Reply via email to