| 1 |
// PR c++/101165 - P2266R1 - Simpler implicit move |
| 2 |
// { dg-do compile { target c++23 } } |
| 3 |
// Tests from P2266R1, decltype-related changes in |
| 4 |
// $ 3.2.1. Interaction with decltype and decltype(auto) |
| 5 |
|
| 6 |
template<typename T, typename U> |
| 7 |
struct same_type { static const bool value = false; }; |
| 8 |
|
| 9 |
template<typename T> |
| 10 |
struct same_type<T, T> { static const bool value = true; }; |
| 11 |
|
| 12 |
auto f1(int x) -> decltype(x) { return (x); } |
| 13 |
static_assert(same_type<decltype(f1), int (int)>::value); |
| 14 |
auto f2(int x) -> decltype((x)) { return (x); } // { dg-error "cannot bind" } |
| 15 |
static_assert(same_type<decltype(f2), int& (int)>::value); |
| 16 |
auto f3(int x) -> decltype(auto) { return (x); } // { dg-warning "reference to local variable" } |
| 17 |
static_assert(same_type<decltype(f3), int&& (int)>::value); |
| 18 |
auto g1(int x) -> decltype(x) { return x; } |
| 19 |
static_assert(same_type<decltype(g1), int (int)>::value); |
| 20 |
auto g2(int x) -> decltype((x)) { return x; } // { dg-error "cannot bind" } |
| 21 |
static_assert(same_type<decltype(g2), int& (int)>::value); |
| 22 |
auto g3(int x) -> decltype(auto) { return x; } |
| 23 |
static_assert(same_type<decltype(g3), int (int)>::value); |
| 24 |
|
| 25 |
// Note that f2 and g2 are well-formed in C++20, but we propose to make |
| 26 |
// f2 and g2 ill-formed, because they attempt to bind an lvalue reference |
| 27 |
// to a move-eligible xvalue expression. |
| 28 |
|
| 29 |
struct X { }; |
| 30 |
|
| 31 |
auto |
| 32 |
f4 (X x) |
| 33 |
{ |
| 34 |
return x; |
| 35 |
} |
| 36 |
static_assert(same_type<decltype(f4), X(X)>::value); |
| 37 |
|
| 38 |
auto& |
| 39 |
f5 (X x) |
| 40 |
{ |
| 41 |
return x; // { dg-error "cannot bind non-const lvalue reference" } |
| 42 |
} |
| 43 |
static_assert(same_type<decltype(f5), X&(X)>::value); |
| 44 |
|
| 45 |
auto&& |
| 46 |
f6 (X x) |
| 47 |
{ |
| 48 |
return x; // { dg-warning "reference to local variable" } |
| 49 |
} |
| 50 |
static_assert(same_type<decltype(f6), X&&(X)>::value); |
| 51 |
|
| 52 |
auto |
| 53 |
f7 (X x) |
| 54 |
{ |
| 55 |
return (x); |
| 56 |
} |
| 57 |
static_assert(same_type<decltype(f7), X(X)>::value); |
| 58 |
|
| 59 |
auto& |
| 60 |
f8 (X x) |
| 61 |
{ |
| 62 |
return (x); // { dg-error "cannot bind non-const lvalue reference" } |
| 63 |
} |
| 64 |
static_assert(same_type<decltype(f8), X&(X)>::value); |
| 65 |
|
| 66 |
auto&& |
| 67 |
f9 (X x) |
| 68 |
{ |
| 69 |
return (x); // { dg-warning "reference to local variable" } |
| 70 |
} |
| 71 |
static_assert(same_type<decltype(f9), X&&(X)>::value); |
| 72 |
|
| 73 |
decltype(auto) |
| 74 |
f10 (X x) |
| 75 |
{ |
| 76 |
return x; |
| 77 |
} |
| 78 |
static_assert(same_type<decltype(f10), X(X)>::value); |
| 79 |
|
| 80 |
decltype(auto) |
| 81 |
f11 (X x) |
| 82 |
{ |
| 83 |
return (x); // { dg-warning "reference to local variable" } |
| 84 |
} |
| 85 |
static_assert(same_type<decltype(f11), X&&(X)>::value); |
| 86 |
|
| 87 |
decltype(auto) |
| 88 |
f12 (X& x) |
| 89 |
{ |
| 90 |
return x; |
| 91 |
} |
| 92 |
static_assert(same_type<decltype(f12), X&(X&)>::value); |
| 93 |
|
| 94 |
decltype(auto) |
| 95 |
f13 (X& x) |
| 96 |
{ |
| 97 |
return (x); |
| 98 |
} |
| 99 |
static_assert(same_type<decltype(f13), X&(X&)>::value); |
| 100 |
|
| 101 |
decltype(auto) |
| 102 |
f14 (X&& x) |
| 103 |
{ |
| 104 |
return x; |
| 105 |
} |
| 106 |
static_assert(same_type<decltype(f14), X&&(X&&)>::value); |
| 107 |
|
| 108 |
decltype(auto) |
| 109 |
f15 (X&& x) |
| 110 |
{ |
| 111 |
return (x); |
| 112 |
} |
| 113 |
static_assert(same_type<decltype(f15), X&&(X&&)>::value); |