public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Marek Polacek <polacek@redhat.com>
Cc: Jason Merrill <jason@redhat.com>,
	GCC Patches <gcc-patches@gcc.gnu.org>,
	 "libstdc++" <libstdc++@gcc.gnu.org>
Subject: Re: [PATCH] c++: Implement __is_{nothrow_,}convertible [PR106784]
Date: Fri, 23 Sep 2022 17:34:21 +0100	[thread overview]
Message-ID: <CACb0b4=1K_W_HpN-R_ZcyO-UCo--wqAmD1Uzj68ujqcH4c+Kfg@mail.gmail.com> (raw)
In-Reply-To: <CACb0b4=JVz4d34pDnRHN4+mo0sAHO35KLu66rKSw068xaPj4vg@mail.gmail.com>

On Fri, 23 Sept 2022 at 15:43, Jonathan Wakely wrote:
>
> On Fri, 23 Sept 2022 at 15:34, Marek Polacek wrote:
> >
> > On Thu, Sep 22, 2022 at 06:14:44PM -0400, Jason Merrill wrote:
> > > On 9/22/22 09:39, Marek Polacek wrote:
> > > > To improve compile times, the C++ library could use compiler built-ins
> > > > rather than implementing std::is_convertible (and _nothrow) as class
> > > > templates.  This patch adds the built-ins.  We already have
> > > > __is_constructible and __is_assignable, and the nothrow forms of those.
> > > >
> > > > Microsoft (and clang, for compatibility) also provide an alias called
> > > > __is_convertible_to.  I did not add it, but it would be trivial to do
> > > > so.
> > > >
> > > > I noticed that our __is_assignable doesn't implement the "Access checks
> > > > are performed as if from a context unrelated to either type" requirement,
> > > > therefore std::is_assignable / __is_assignable give two different results
> > > > here:
> > > >
> > > >    class S {
> > > >      operator int();
> > > >      friend void g(); // #1
> > > >    };
> > > >
> > > >    void
> > > >    g ()
> > > >    {
> > > >      // #1 doesn't matter
> > > >      static_assert(std::is_assignable<int&, S>::value, "");
> > > >      static_assert(__is_assignable(int&, S), "");
> > > >    }
> > > >
> > > > This is not a problem if __is_assignable is not meant to be used by
> > > > the users.
> > >
> > > That's fine, it's not.
> >
> > Okay then.  libstdc++ needs to make sure then that it's handled right.
>
> That's fine, the type traits in libstdc++ are always "a context
> unrelated to either type", unless users do something idiotic like
> declare std::is_assignable as a friend.
>
> https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1339r1.pdf
> wants to explicitly say that's idiotic.

And I just checked that a variable template like std::is_assignable_v
also counts as "a context unrelated to either type", even when
instantiated inside a member function of the type:

#include <type_traits>

template<typename T, typename U>
constexpr bool is_assignable_v = __is_assignable(T, U);

class S {
  operator int();
  friend void g(); // #1
};

void
g ()
{
  // #1 doesn't matter
  static_assert(std::is_assignable<int&, S>::value, "");
  static_assert(std::is_assignable_v<int&, S>, "");
  static_assert(__is_assignable(int&, S), "");
}

The first two assertions are consistent, and fail, which is what we
want.  The direct use of the built-in succeeds, but we don't care.


  reply	other threads:[~2022-09-23 16:34 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-22 13:39 Marek Polacek
2022-09-22 22:14 ` Jason Merrill
2022-09-23 14:34   ` Marek Polacek
2022-09-23 14:43     ` Jonathan Wakely
2022-09-23 16:34       ` Jonathan Wakely [this message]
2022-09-23 16:37         ` Marek Polacek
2022-09-23 15:54     ` Jason Merrill
2022-09-23 16:16       ` Marek Polacek
2022-09-23 14:40   ` Jonathan Wakely
2022-09-23 15:04     ` Marek Polacek

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='CACb0b4=1K_W_HpN-R_ZcyO-UCo--wqAmD1Uzj68ujqcH4c+Kfg@mail.gmail.com' \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=polacek@redhat.com \
    /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).