public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "dangelog at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-107525-4@http.gcc.gnu.org/bugzilla/> (raw)

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?

             reply	other threads:[~2022-11-04 11:38 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-04 11:38 dangelog at gmail dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-107525-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).