| 1 |
// PR c++/101165 - P2266R1 - Simpler implicit move |
| 2 |
// { dg-do compile { target c++23 } } |
| 3 |
// { dg-options "-Wdangling-reference" } |
| 4 |
|
| 5 |
struct X { |
| 6 |
X (); |
| 7 |
X(X&&); |
| 8 |
}; |
| 9 |
|
| 10 |
X&& rref (); |
| 11 |
|
| 12 |
X&& |
| 13 |
f1 (X&& x) |
| 14 |
{ |
| 15 |
return x; |
| 16 |
} |
| 17 |
|
| 18 |
template<typename T> T&& |
| 19 |
f2 (T&& x) |
| 20 |
{ |
| 21 |
return x; |
| 22 |
} |
| 23 |
template X& f2<X&>(X&); |
| 24 |
template X&& f2<X>(X&&); |
| 25 |
|
| 26 |
X&& |
| 27 |
f3 () |
| 28 |
{ |
| 29 |
X&& x = rref (); |
| 30 |
return x; |
| 31 |
} |
| 32 |
|
| 33 |
void |
| 34 |
f4 () |
| 35 |
try { |
| 36 |
X x; |
| 37 |
throw x; |
| 38 |
} catch (...) { } |
| 39 |
|
| 40 |
void |
| 41 |
f5 () |
| 42 |
{ |
| 43 |
auto l1 = [](auto x) -> auto { return x; }; |
| 44 |
auto &&x1 = l1(X{}); |
| 45 |
auto l2 = [](auto x) -> auto& { return x; }; // { dg-error "cannot bind non-const lvalue reference" } |
| 46 |
auto &&x2 = l2(X{}); |
| 47 |
auto l3 = [](auto x) -> auto&& { return x; }; // { dg-warning "reference to local" } |
| 48 |
auto &&x3 = l3(X{}); |
| 49 |
} |
| 50 |
|
| 51 |
constexpr int & |
| 52 |
f6 (int &&n) |
| 53 |
{ |
| 54 |
return n; // { dg-error "cannot bind non-const lvalue reference" } |
| 55 |
} |
| 56 |
|
| 57 |
void |
| 58 |
do_f6 () |
| 59 |
{ |
| 60 |
auto x = f6 (42); |
| 61 |
} |
| 62 |
|
| 63 |
template<typename T> auto & |
| 64 |
f7 (T &&t) |
| 65 |
{ |
| 66 |
return t; // { dg-error "cannot bind non-const lvalue reference" } |
| 67 |
} |
| 68 |
|
| 69 |
void |
| 70 |
do_f7 () |
| 71 |
{ |
| 72 |
const int &x = f7 (0); // { dg-warning "dangling reference" } |
| 73 |
} |