From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id D0C683857804; Tue, 15 Dec 2020 13:58:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D0C683857804 From: "matthieum.147192 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/98288] New: Accidental equality of classes templated by pointer to local static constant of templated function Date: Tue, 15 Dec 2020 13:58:54 +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: 7.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: matthieum.147192 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 15 Dec 2020 13:58:54 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98288 Bug ID: 98288 Summary: Accidental equality of classes templated by pointer to local static constant of templated function Product: gcc Version: 7.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: matthieum.147192 at gmail dot com Target Milestone: --- Based on https://stackoverflow.com/q/65306562/147192. The behavior of GCC (and Clang) is inconsistent (between compile-time and run-time), however it is unclear to me whether the inconsistency is conform= ing with the C++17 standard or not. The following reduced program is expected to return 0 (invoking g++ with -std=3Dc++17): template struct is_same { static constexpr bool value =3D false; }; template struct is_same { static constexpr bool value =3D true; }; template static constexpr bool is_same_v =3D is_same::value; using uintptr_t =3D unsigned long long; template struct Parameterized { int const* member; }; template auto create() { static constexpr int const I =3D 2; return Parameterized<&I>{ &I }; } int main() { auto one =3D create(); auto two =3D create(); if (is_same_v) { return reinterpret_cast(one.member) =3D=3D reinterpret_cast(two.member) ? 1 : 2; } return 0; } Yet, on all versions of GCC where it compiles (from 7.2 onwards), and for a= ll optimization levels (from -O0 to -O3), it returns 2, indicating: - That `one` and `two` have the same type -- which according to 17.4 [temp.type] should mean that they point to the same object. - Yet they point to different objects -- there are two instances of `create()::I`, one for `T =3D short` and one for `T =3D int`. The assembly listing clearly contains 2 different instances of `create()::I`. Notes: - If `I` is initialized with `=3D sizeof(T)`, instead, then the program ret= urns 0 as expected. - Clang (up to 11.0) fails to compile the program with -O0 (the linker fail= ing to find a `create()::I` variable), and otherwise produces the same result as GCC with -O1 to -O3.=