diff mbox

C++ PATCH for c++/59296 (rvalue object and lvalue ref-qualifier)

Message ID 53A20ED2.6070306@redhat.com
State New
Headers show

Commit Message

Jason Merrill June 18, 2014, 10:12 p.m. UTC
We were treating a const & member function like a normal const 
reference, and binding an rvalue object argument to it.  But it doesn't 
work that way.

Tested x86_64-pc-linux-gnu, applying to trunk.

Comments

Marc Glisse June 19, 2014, 11:29 a.m. UTC | #1
On Thu, 19 Jun 2014, Jason Merrill wrote:

> We were treating a const & member function like a normal const reference, and 
> binding an rvalue object argument to it.  But it doesn't work that way.

That looks weird to me. The const&& version is a better match than the 
const&, so we should pick that one in overload resolution, but if we 
remove the const&& version, the other one seems valid to me, we shouldn't 
reject this program:

struct Type
{
   void get() const& { }
};

int main()
{
   Type{}.get();
}

At least both clang and intel accept it, and I hope the standard didn't 
make it behave differently from regular function calls.
Jason Merrill June 20, 2014, 1:11 p.m. UTC | #2
On 06/19/2014 01:29 PM, Marc Glisse wrote:
> That looks weird to me. The const&& version is a better match than the
> const&, so we should pick that one in overload resolution, but if we
> remove the const&& version, the other one seems valid to me

Hmm, you're right, I was confused.

Jason
diff mbox

Patch

commit 20a165532a9b0b0dada391716a1fb781af3ec005
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Jun 18 22:56:25 2014 +0200

    	PR c++/59296
    	* call.c (add_function_candidate): Set LOOKUP_NO_RVAL_BIND for
    	ref-qualifier handling.

diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 1d4c4f9..b4adf36 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -2025,6 +2025,8 @@  add_function_candidate (struct z_candidate **candidates,
 		     object parameter has reference type.  */
 		  bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
 		  parmtype = cp_build_reference_type (parmtype, rv);
+		  /* Don't bind an rvalue to a const lvalue ref-qualifier.  */
+		  lflags |= LOOKUP_NO_RVAL_BIND;
 		}
 	      else
 		{
diff --git a/gcc/testsuite/g++.dg/cpp0x/ref-qual15.C b/gcc/testsuite/g++.dg/cpp0x/ref-qual15.C
new file mode 100644
index 0000000..ca333c2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/ref-qual15.C
@@ -0,0 +1,13 @@ 
+// PR c++/59296
+// { dg-do compile { target c++11 } }
+
+struct Type
+{
+  void get() const& { }
+  void get() const&& { }
+};
+
+int main()
+{
+  Type{}.get();
+}