public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/107525] New: propagate_const should not be using SFINAE on its conversion operators
@ 2022-11-04 11:38 dangelog at gmail dot com
  2022-11-04 13:59 ` [Bug libstdc++/107525] " redi at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: dangelog at gmail dot com @ 2022-11-04 11:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107525

            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_observers

> constexpr operator const element_type*() const;
> 
> Constraints:
>    T is an object pointer type or has an implicit conversion to const element_type*. 
> 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 now a
function template, and that means that https://eel.is/c++draft/over.ics.user#3
kicks in:

> If the user-defined conversion is specified by a specialization of a conversion function template, the second standard conversion sequence shall have exact match rank.


Concretely, this means that for instance we lose implicit conversions towards
base classes of the pointed-to type:

    std::experimental::propagate_const<Derived *> ptr;

    Derived *d1 = ptr;  // Convert precisely to Derived *: OK
    Base *b1    = ptr;  // Convert to a pointer to a base: ERROR

    Base *b2    = static_cast<Derived *>(ptr); // OK
    Base *b3    = static_cast<Base *>(ptr);    // ERROR

    Base *b4    = 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, but 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?

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-11-05 14:04 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-04 11:38 [Bug libstdc++/107525] New: propagate_const should not be using SFINAE on its conversion operators dangelog at gmail dot com
2022-11-04 13:59 ` [Bug libstdc++/107525] " redi at gcc dot gnu.org
2022-11-04 14:07 ` redi at gcc dot gnu.org
2022-11-04 14:26 ` redi at gcc dot gnu.org
2022-11-04 14:43 ` dangelog at gmail dot com
2022-11-04 15:04 ` redi at gcc dot gnu.org
2022-11-04 15:09 ` redi at gcc dot gnu.org
2022-11-04 15:10 ` redi at gcc dot gnu.org
2022-11-04 15:15 ` ville.voutilainen at gmail dot com
2022-11-04 15:58 ` dangelog at gmail dot com
2022-11-05 14:01 ` cvs-commit at gcc dot gnu.org
2022-11-05 14:04 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).