[Bug c++/94923] False positive -Wclass-memaccess with trivially copyable std::optional

daniel.kruegler at googlemail dot com gcc-bugzilla@gcc.gnu.org
Sat May 2 15:38:07 GMT 2020


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

Daniel Krügler <daniel.kruegler at googlemail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |daniel.kruegler@googlemail.
                   |                            |com

--- Comment #1 from Daniel Krügler <daniel.kruegler at googlemail dot com> ---
Corrected code without typos and including the necessary header files:

```
#include <type_traits>
#include <optional>
#include <cstddef>
#include <cstring>

static_assert(std::is_trivially_copyable_v<std::optional<char>>);

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);
}

int main() {}
```


More information about the Gcc-bugs mailing list