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

            Bug ID: 98835
           Summary: False positive -Wclass-memaccess with class with
                    ref-qualified copy-assignment operator
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jchl at arista dot com
  Target Milestone: ---

Consider the following code:

  #include <type_traits>
  #include <cstring>

  class Good {
   public:
      Good & operator=(Good const &) = default;
  };

  class Bad {
   public:
      Bad & operator=(Bad const &) & = default;
  };

  template<typename T>
  void test() {
      static_assert(std::is_trivially_copyable_v<T>);
      T value1;
      T value2;
      std::memcpy(&value1, &value2, sizeof(T));
  }

  int main() {
      test<Good>();
      test<Bad>();
  }

[See: https://godbolt.org/z/4vj9GT ]

GCC trunk on x86_64 incorrectly gives the following warning:

  <source>: In instantiation of 'void test() [with T = Bad]':
  <source>:24:15:   required from here
  <source>:19:16: warning: 'void* memcpy(void*, const void*, size_t)' writing
to an object of type 'class Bad' with no trivial copy-assignment; use
copy-assignment or copy-initialization instead [-Wclass-memaccess]
     19 |     std::memcpy(&value1, &value2, sizeof(T));
        |     ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Since both Good and Bad have trivial copy-assignment operators and are
trivially copyable, both types are eligible to be memcpy'd; the
ref-qualification of the assignment operator shouldn't be relevant.

Reply via email to