https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107361
Bug ID: 107361 Summary: Why does -Wclass-memaccess require trivial types, instead of trivially-copyable types? Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: carlosgalvezp at gmail dot com Target Milestone: --- I find -Wclass-memaccess warns on this kind of code: #include <cstring> struct Foo { int x{}; char y{}; int z{}; }; int main() { Foo a; std::memset(&a, 0, sizeof(a)); } All because Foo has default member initializers, making it a non-trivial type. It's still trivially-copyable, which is the requirement for std::memset. Therefore I ask: why is the warning stricter than it needs to be? Making sure structs are initialized is defensive programming, and consistent with coding guidelines that require classes to initialize their data members. Also, there are cases where we cannot use the C++ initialization (Foo a = {};). For example, this class has implicit padding, and the C++ initialization will not initialize that. This is a problem for serializer/deserializer type of code (copying/reading uninitialized padding bytes, valgrind will complain).