From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 7BB7F3858D28; Sun, 23 Oct 2022 07:22:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7BB7F3858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666509724; bh=A7aC2kwlFrjuUtYeFh2bhszh32V9d63JJ/Hv3VDeW3A=; h=From:To:Subject:Date:From; b=eZGd9g+3JvflGDVgNkj08j4xUhJPKYOoAMJKLJDmJd9VMS5zZaDe/TQ6pvs6kk254 OlpZ0OZpgKi8iUStzf1c8vT6yjScM18A4xNShE3Y/hEWpRs9efxlH3iPQOmKQRKvOZ kI9JoFc1A7dhywdnwzcFLphu2NqXbEick70P/OJ0= From: "carlosgalvezp at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/107361] New: Why does -Wclass-memaccess require trivial types, instead of trivially-copyable types? Date: Sun, 23 Oct 2022 07:22:03 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 13.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: carlosgalvezp at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107361 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 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 ty= pe. 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 member= s.=20 Also, there are cases where we cannot use the C++ initialization (Foo a =3D= {};). For example, this class has implicit padding, and the C++ initialization wi= ll not initialize that. This is a problem for serializer/deserializer type of = code (copying/reading uninitialized padding bytes, valgrind will complain).=