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