From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 840983858407; Fri, 4 Nov 2022 11:38:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 840983858407 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667561895; bh=1c3w9l7GR6uCv3S8HFDUC2GDvvPxco6nCoi0EN5d3x8=; h=From:To:Subject:Date:From; b=UDX0FtnrOC8ewbbx2sqMd6X6XSyOo14fawH5sZMCfNRq0vzw/wXI9zRad+B+7UUAo 6IsPjYFQVtechnoZx/2OIdJuR/L1ITA8ogoSz1RXejPYs1UTkI4mSFiSg9p+l0jp7O 1k5f26PyjRfA7PocurzigaqInLh0ZJ2YIK5ZTQ/s= From: "dangelog at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/107525] New: propagate_const should not be using SFINAE on its conversion operators Date: Fri, 04 Nov 2022 11:38:07 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 12.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: dangelog 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=3D107525 Bug ID: 107525 Summary: propagate_const should not be using SFINAE on its conversion operators Product: gcc Version: 12.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: dangelog at gmail dot com Target Milestone: --- propagate_const in the LFTSv3 has implicit conversion operators which have constraints on them: https://cplusplus.github.io/fundamentals-ts/v3.html#propagate_const.const_o= bservers > constexpr operator const element_type*() const; >=20 > Constraints: > T is an object pointer type or has an implicit conversion to const ele= ment_type*.=20 > Returns: > get(). libstdc++ implements these constraints by means of SFINAE on the operators. This is user-hostile: using SFINAE means that the conversion operator is no= w a function template, and that means that https://eel.is/c++draft/over.ics.use= r#3 kicks in: > If the user-defined conversion is specified by a specialization of a conv= ersion function template, the second standard conversion sequence shall hav= e exact match rank. Concretely, this means that for instance we lose implicit conversions towar= ds base classes of the pointed-to type: std::experimental::propagate_const ptr; Derived *d1 =3D ptr; // Convert precisely to Derived *: OK Base *b1 =3D ptr; // Convert to a pointer to a base: ERROR Base *b2 =3D static_cast(ptr); // OK Base *b3 =3D static_cast(ptr); // ERROR Base *b4 =3D ptr.get(); // OK But these should all work. The design of propagate_const is for it to be "drop-in replacement", maximizing compatibility with existing code. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4388.html says explictly "When T is an object pointer type operator value* exists and allows implicit conversion to a pointer. This avoids using get to access the pointer in contexts where it was unnecesary before addition of the propagate_const wrapper." -- So. ideally, the conversion operators should be using C++20 constraints, bu= t of course that's not possible. I guess that a reasonable alternative would be = to isolate them in a base class, and apply SFINAE on that base class instead?=