This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
C++ PATCH for c++/35548 (dangling reference with ?:)
- From: Jason Merrill <jason at redhat dot com>
- To: gcc-patches List <gcc-patches at gcc dot gnu dot org>
- Date: Mon, 17 Mar 2008 22:48:21 -0400
- Subject: C++ PATCH for c++/35548 (dangling reference with ?:)
DR391 requires us to bind references directly to temporaries when
appropriate, but we still need to respect LOOKUP_NO_TEMP_BIND.
Tested x86_64-pc-linux-gnu, applied to trunk and 4.3.
2008-03-17 Jason Merrill <jason@redhat.com>
PR c++/35548
* call.c (reference_binding): Check LOOKUP_NO_TEMP_BIND when binding
a temp directly to a reference as per DR391.
Index: cp/call.c
===================================================================
*** cp/call.c (revision 132953)
--- cp/call.c (working copy)
*************** reference_binding (tree rto, tree rfrom,
*** 1145,1151 ****
const and rvalue references to rvalues of compatible class type. */
if (compatible_p
&& (lvalue_p
! || ((CP_TYPE_CONST_NON_VOLATILE_P(to) || TYPE_REF_IS_RVALUE (rto))
&& CLASS_TYPE_P (from))))
{
/* [dcl.init.ref]
--- 1145,1152 ----
const and rvalue references to rvalues of compatible class type. */
if (compatible_p
&& (lvalue_p
! || (!(flags & LOOKUP_NO_TEMP_BIND)
! && (CP_TYPE_CONST_NON_VOLATILE_P(to) || TYPE_REF_IS_RVALUE (rto))
&& CLASS_TYPE_P (from))))
{
/* [dcl.init.ref]
Index: testsuite/g++.dg/init/ref16.C
===================================================================
*** testsuite/g++.dg/init/ref16.C (revision 0)
--- testsuite/g++.dg/init/ref16.C (revision 0)
***************
*** 0 ****
--- 1,23 ----
+ // PR c++/35548
+ // { dg-do run }
+
+ int c;
+ struct A
+ {
+ A() { ++c; }
+ A(const A&) { ++c; }
+ ~A() { --c; }
+ };
+
+ A f()
+ {
+ return A();
+ }
+
+ int i;
+ const A* ap;
+ int main()
+ {
+ const A& ar = i ? *ap : f();
+ return (c == 0);
+ }