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

            Bug ID: 111094
           Summary: Spurious Wuninitialized swapping underlying bytes of
                    object representation in move constructor
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ed at catmur dot uk
  Target Milestone: ---

struct S { short x, y; };
struct A {
    A() {}
    A(A&& rhs) {
        auto p = reinterpret_cast<unsigned char*>(&s);
        auto q = reinterpret_cast<unsigned char*>(&rhs.s);
        for (int i = 0; i != sizeof s; ++i) {
            auto t = p[i];
            p[i] = q[i];
            q[i] = t;
        }
    }
    bool b = false;
    S s;
};
A f() {
    A a;
    A b = static_cast<A&&>(a);
    return b;
}

at -O3 -Wall:

In constructor 'A::A(A&&)',
    inlined from 'A f()' at <source>:18:29:
<source>:9:23: warning: '*(__vector(4) unsigned char*)((char*)&a + offsetof(A,
A::s.S::x))' is used uninitialized [-Wuninitialized]
    9 |             p[i] = q[i];
      |                    ~~~^
<source>: In function 'A f()':
<source>:17:7: note: 'a' declared here
   17 |     A a;
      |       ^

I'm reasonably sure that this usage of swapping underlying bytes is OK by
[basic.indet]/2.3.

(Motivation is e.g. optional with empty-on-move behavior and optimization for
trivial types.)

Reply via email to