From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E665F3870887; Sat, 25 Mar 2023 11:55:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E665F3870887 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679745321; bh=RAdzRwlv9rapHKMbwW2ETQW6QVy2PTSl2Iz0ssQLcP8=; h=From:To:Subject:Date:From; b=P9tQjVNPybD4KqLdCHNHlDrVGh1EFswJr/dV9jVEjMt3GH4qeA/Yx9HjdQi1HSFu2 CKljP9VC+pwO9Gse+nCOtc+8xnve5rZEQfQC+DYRl8yuLup3XaVTVEPjZaTTAvTpJN qjYEnDRtBzvgiGFdBO4VBftqZNZf3LxOCNQyY8Xs= From: "vincenzo.innocente at cern dot ch" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/109281] New: use std::optional results in suboptimal code Date: Sat, 25 Mar 2023 11:55:21 +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: 12.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: vincenzo.innocente at cern dot ch 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=3D109281 Bug ID: 109281 Summary: use std::optional results in suboptimal code Product: gcc Version: 12.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: vincenzo.innocente at cern dot ch Target Milestone: --- In the following (almost real) code gcc emits suboptimal code if std::optio= nal is used w/r/t home made one and clang see https://godbolt.org/z/Pba51Ye7Y ----- code #include // #define USE_OPTIONAL #ifdef USE_OPTIONAL struct SubRingCrossings { SubRingCrossings(int ci, int ni, float nd) : closestIndex(ci), nextIndex(= ni), nextDistance(nd) {} int closestIndex; int nextIndex; float nextDistance; }; #else struct SubRingCrossings { SubRingCrossings() : valid(false) {} SubRingCrossings(int ci, int ni, float nd) : valid(true), closestIndex(ci= ), nextIndex(ni), nextDistance(nd) {} bool valid; int closestIndex; int nextIndex; float nextDistance; }; #endif bool condition(); #ifdef USE_OPTIONAL std::optional foo() { if (condition()) { return std::nullopt; } return SubRingCrossings(1, 2, 3.14); } #else SubRingCrossings foo() { if (condition()) { return SubRingCrossings(); } return SubRingCrossings(1, 2, 3.14); } #endif int bar() { auto tmp =3D foo(); #ifdef USE_OPTIONAL if (tmp) { return tmp->closestIndex; #else if (tmp.valid) { return tmp.closestIndex; #endif } else { return 0; } }=