public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/94923] New: False positive -Wclass-memaccess with trivially copyable std::optional
@ 2020-05-02 13:46 dawid_jurek at vp dot pl
  2020-05-02 15:38 ` [Bug c++/94923] " daniel.kruegler at googlemail dot com
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: dawid_jurek at vp dot pl @ 2020-05-02 13:46 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94923
           Summary: False positive -Wclass-memaccess with trivially
                    copyable std::optional
           Product: gcc
           Version: 9.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dawid_jurek at vp dot pl
  Target Milestone: ---

Consider following C++ snippet:

static_assert(std::is_trivially_copyable_v<std::optional<char>>);
static void not_ok() {
  std::optional<char> value;
  std::byte buf[128;
  std::memcpy(&buf[0], &value, sizeof value);
  std::memcpy(&value, &buf[0], sizeof value);
}

With above snippet fresh x86-64 gcc trunk reports following warning:

source>: In function 'void not_ok()':

<source>:27:46: warning: 'void* memcpy(void*, const void*, size_t)' copying an
object of non-trivial type 'class std::optional<char>' from an array of 'enum
class std::byte' [-Wclass-memaccess]

   27 |     std::memcpy(&value, &buf[0], sizeof value);

[basic.types]/2 explicitly permits for such round trip for every trivially
copyable T via std::memcpy even if T is not trivial.
Also clang accept such code without complains (take a look at link below).
Changing std::byte buf[128; to char buf[128]; suppress the warning so seems
that std::byte is relevant.
You can find full source code here https://godbolt.org/z/JrcnS5

Regards,
Dawid

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

end of thread, other threads:[~2022-03-17 19:53 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-02 13:46 [Bug c++/94923] New: False positive -Wclass-memaccess with trivially copyable std::optional dawid_jurek at vp dot pl
2020-05-02 15:38 ` [Bug c++/94923] " daniel.kruegler at googlemail dot com
2020-05-02 17:59 ` dawid_jurek at vp dot pl
2020-05-02 22:34 ` msebor at gcc dot gnu.org
2020-05-02 22:36 ` msebor at gcc dot gnu.org
2020-05-08 18:58 ` msebor at gcc dot gnu.org
2020-05-19 19:09 ` cvs-commit at gcc dot gnu.org
2020-05-21  8:31 ` dawid_jurek at vp dot pl
2022-03-17 19:53 ` msebor 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).