public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Marek Polacek <polacek@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: C++ PATCH/RFC for c++/87109, wrong overload with ref-qualifiers
Date: Thu, 30 Aug 2018 13:22:00 -0000	[thread overview]
Message-ID: <CADzB+2m_UXzVKXCAB0hosjigPt0u5w++g3+0i0W_e99yUc5r5w@mail.gmail.com> (raw)
In-Reply-To: <20180830000345.GB12638@redhat.com>

On Wed, Aug 29, 2018 at 8:03 PM, Marek Polacek <polacek@redhat.com> wrote:
> I've now gotten to the point where I question the validity of this PR, so it's
> probably a good time to stop and ask for some advice.
>
> As discussed in <https://gcc.gnu.org/ml/gcc-patches/2018-08/msg01607.html>, we
> choose the wrong overload for f1:
>
> struct C { };
> struct A {
>   operator C() &;
>   operator C() &&;
> };
>
> C f1(A a)
> {
>    return a; // should call operator C()&, but calls operator C()&&
> }
>
> Since we're returning a local variable, we know it's about to be destroyed,
> so even though it's got a name, not for very much longer, so we activate move
> semantics.  So we perform overload resolution with 'a' turned into
> *NON_LVALUE_EXPR<(A&) &a>, an xvalue.  We need to convert 'a' from A to C,
> which is taking place in build_user_type_conversion_1.  It will see two
> cadidates:
>
>   A::operator C() &&
>   A::operator C() &
>
> when adding these candidates in add_function_candidate we process the
> ref-qualifiers by tweaking the type of the implicit object parameter by turning
> it into a reference type.  Then we create an implicit conversion sequence
> for converting the type of the argument to the type of the parameter,
> so A to A&.  That succeeds in the first case (an xvalue binding to an rvalue
> reference) but fails in the second case (an xvalue binding to an lvalue
> reference).  And thus we end up using the first overload.
>
> But why is this invalid, again?  [class.copy.elision] says "or if the type of
> the first parameter of the selected constructor is not an rvalue reference to
> the object's type (possibly cv-qualified), overload resolution is performed
> again, considering the object as an lvalue." but I don't see how that applies
> here.  (Constructors can't have ref-qualifiers anyway.)
>
> Thoughts?

Where that rule comes in is when we choose the constructor for C:
since we've already called operator C()&&, we choose C(C&&), which
does not have a first parameter of "rvalue reference to cv A", so it
should be rejected.

Jason

  reply	other threads:[~2018-08-30 13:22 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-30  0:03 Marek Polacek
2018-08-30 13:22 ` Jason Merrill [this message]
2018-09-04 19:02   ` Marek Polacek
2018-09-04 21:29     ` Jason Merrill
2018-09-05 20:09       ` Marek Polacek
2018-09-05 20:56         ` Jason Merrill

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=CADzB+2m_UXzVKXCAB0hosjigPt0u5w++g3+0i0W_e99yUc5r5w@mail.gmail.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@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).