2 // { dg-do compile { target c++11 } }
3 // { dg-options "-Wdangling-reference" }
5 const int& f(const int& i) { return i; }
6 const int& f_(const int& i) { return i; }
8 const int& rp(const int *);
10 const int& globref(const int&) { return g; }
13 operator const int&() const { return *i; }
17 const int& r1 = f(10); // { dg-warning "dangling reference" }
18 // r2 = _ZGR2r2_ = (int) *f ((const int &) &TARGET_EXPR <D.2429, 10>) + 1; (const int &) &_ZGR2r2_
19 const int& r2 = f(10) + 1;
20 // Don't warn here, we have
21 // r3 = f (X::operator const int& (&x))
23 // Don't warn here, because we've seen the definition of globref
24 // and could figure out that it may not return one of its parms.
25 // Questionable -- it can also hide bugs --, but it helps here.
26 const int& r4 = globref(1);
27 const int& r5 = (42, f(10)); // { dg-warning "dangling reference" }
28 const int& r6 = (f(10), 42);
29 const int& r7 = (f(10)); // { dg-warning "dangling reference" }
30 const int& r8 = g ? f(10) : f(9); // { dg-warning "dangling reference" }
31 const int& r9 = (42, g ? f(10) : f(9)); // { dg-warning "dangling reference" }
32 const int& r10 = (g ? f(10) : f(9), 42);
33 // Binds to a reference temporary for r11. No dangling reference.
34 const int& r11 = g ? f(10) : 9;
35 const int& r12 = g ? 9 : f(10);
36 // r12 = f (f ((const int &) &TARGET_EXPR <D.2459, 1>))
37 const int& r13 = f(f(1)); // { dg-warning "dangling reference" }
38 const int& r14 = f(f_(1)); // { dg-warning "dangling reference" }
39 const int& r15 = f(g ? f(1) : f(2)); // { dg-warning "dangling reference" }
40 const int& r16 = f(*&f(1)); // { dg-warning "dangling reference" }
41 const int& r17 = rp(&f(1));
42 const int& r18 = rp(&f(g));
43 const int& r19 = h(f(1));
44 // Other forms of initializers.
45 const int& r20(f(10)); // { dg-warning "dangling reference" }
46 const int& r21(f(10)); // { dg-warning "dangling reference" }
47 // Returns a ref, but doesn't have a parameter of reference type.
48 const int& r22 = h(10);
49 const int& r23 = g ? h(10) : f(10); // { dg-warning "dangling reference" }
50 const int& r24 = g ? f(10) : h(10); // { dg-warning "dangling reference" }
51 const int& r25 = g ? h(10) : (1, f(10)); // { dg-warning "dangling reference" }
52 const int& r26 = g ? (1, f(10)) : h(10); // { dg-warning "dangling reference" }
53 const int& r29 = f((f_(1), 1)); // { dg-warning "dangling reference" }
54 const int& r30 = f((f_(1), g));
57 operator int() { return 42; }
60 const int& r27 = f(Z()); // { dg-warning "dangling reference" }
61 const int& r28 = f(true ? Z() : Z()); // { dg-warning "dangling reference" }
63 const int& operator|(const int &, Z);
64 const int& r31 = 1 | Z(); // { dg-warning "dangling reference" }
66 // OK: the reference is bound to the 10 so still valid at the point
67 // where it's copied into i1.
73 const int &lr = f(10); // { dg-warning "dangling reference" }
89 const B& frotz(const D&);
90 const B& b1 = frotz(C{}.d); // { dg-warning "dangling reference" }
91 const B& b2 = frotz(D{}); // { dg-warning "dangling reference" }
92 const B& b3 = frotz(c.d);
93 const B& b4 = frotz(d);
94 const B& b5 = frotz(U{}[0]); // { dg-warning "dangling reference" }
96 // Try returning a subobject.
97 const B& bar (const D& d) { return d; }
98 const B& b6 = bar (D{}); // { dg-warning "dangling reference" }
99 const B& baz (const C& c) { return c.d; }
100 const B& b7 = baz (C{}); // { dg-warning "dangling reference" }
101 const D& qux (const C& c) { return c.d; }
102 const D& d1 = qux (C{}); // { dg-warning "dangling reference" }
107 const E& operator*(const E&);
108 const E& b8 = *E(1); // { dg-warning "dangling reference" }
110 struct F : virtual B { };
111 struct G : virtual B { };
113 const B& yum (const F& f) { return f; }
114 const B& b9 = yum (F{}); // { dg-warning "dangling reference" }
115 const B& lox (const H& h) { return h; }
116 const B& b10 = lox (H{}); // { dg-warning "dangling reference" }
119 const int &r; // { dg-warning "dangling reference" }
120 S() : r(f(10)) { } // { dg-message "created" }
123 // From cppreference.
125 const T& max(const T& a, const T& b)
127 return (a < b) ? b : a;
131 const int& refmax = max(n - 1, n + 1); // { dg-warning "dangling reference" }
137 const int& foo(const int&);
140 // x1 = Y::operator int&& (&TARGET_EXPR <D.2410, {}>)
141 int&& x1 = Y(); // { dg-warning "dangling reference" }
142 int&& x2 = Y{}; // { dg-warning "dangling reference" }
143 int& x3 = Y(); // { dg-bogus "dangling reference" }
144 int& x4 = Y{}; // { dg-bogus "dangling reference" }
145 const int& t1 = Y().foo(10); // { dg-warning "dangling reference" }