public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/98835] New: False positive -Wclass-memaccess with class with ref-qualified copy-assignment operator
@ 2021-01-26  9:33 jchl at arista dot com
  2021-01-26 19:38 ` [Bug c++/98835] " msebor at gcc dot gnu.org
  2021-02-01 15:48 ` cvs-commit at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: jchl at arista dot com @ 2021-01-26  9:33 UTC (permalink / raw)
  To: gcc-bugs

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.

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

end of thread, other threads:[~2021-02-01 15:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-26  9:33 [Bug c++/98835] New: False positive -Wclass-memaccess with class with ref-qualified copy-assignment operator jchl at arista dot com
2021-01-26 19:38 ` [Bug c++/98835] " msebor at gcc dot gnu.org
2021-02-01 15:48 ` cvs-commit at gcc dot gnu.org

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).