| 1 |
// PR c++/101165 - P2266R1 - Simpler implicit move |
| 2 |
// { dg-do compile { target c++23 } } |
| 3 |
// Tests from P2266R1. |
| 4 |
|
| 5 |
namespace std { |
| 6 |
template<typename _Tp> |
| 7 |
struct remove_reference |
| 8 |
{ typedef _Tp type; }; |
| 9 |
|
| 10 |
template<typename _Tp> |
| 11 |
struct remove_reference<_Tp&> |
| 12 |
{ typedef _Tp type; }; |
| 13 |
|
| 14 |
template<typename _Tp> |
| 15 |
struct remove_reference<_Tp&&> |
| 16 |
{ typedef _Tp type; }; |
| 17 |
|
| 18 |
template<typename _Tp> |
| 19 |
constexpr typename std::remove_reference<_Tp>::type&& |
| 20 |
move(_Tp&& __t) noexcept |
| 21 |
{ return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); } |
| 22 |
} |
| 23 |
|
| 24 |
template<typename T, typename U> |
| 25 |
struct same_type { static const bool value = false; }; |
| 26 |
|
| 27 |
template<typename T> |
| 28 |
struct same_type<T, T> { static const bool value = true; }; |
| 29 |
|
| 30 |
struct Widget { |
| 31 |
Widget(Widget&&); |
| 32 |
}; |
| 33 |
|
| 34 |
struct RRefTaker { |
| 35 |
RRefTaker(Widget&&); |
| 36 |
}; |
| 37 |
|
| 38 |
struct Mutt { |
| 39 |
operator int*() &&; |
| 40 |
}; |
| 41 |
|
| 42 |
struct Jeff { |
| 43 |
operator int&() &&; |
| 44 |
}; |
| 45 |
|
| 46 |
struct Ella { |
| 47 |
operator int() &&; |
| 48 |
}; |
| 49 |
|
| 50 |
Widget one(Widget w) { |
| 51 |
return w; // OK since C++11 |
| 52 |
} |
| 53 |
|
| 54 |
RRefTaker two(Widget w) { |
| 55 |
return w; // OK since C++11 + CWG1579 |
| 56 |
} |
| 57 |
|
| 58 |
RRefTaker three(Widget&& w) { |
| 59 |
return w; // OK since C++20 because P0527 |
| 60 |
} |
| 61 |
|
| 62 |
// Tests that implicit move applies even to functions that return references. |
| 63 |
Widget&& four(Widget&& w) { |
| 64 |
return w; // OK since C++23 |
| 65 |
} |
| 66 |
|
| 67 |
// ... or pointers. |
| 68 |
int* five(Mutt x) { |
| 69 |
return x; // OK since C++20 because P1155 |
| 70 |
} |
| 71 |
|
| 72 |
int& six(Jeff x) { |
| 73 |
return x; |
| 74 |
} |
| 75 |
|
| 76 |
int test_ella(Ella e) { |
| 77 |
return e; |
| 78 |
} |
| 79 |
|
| 80 |
template<class T> |
| 81 |
T&& seven(T&& x) { return x; } |
| 82 |
|
| 83 |
void test_seven(Widget w) { |
| 84 |
Widget& r = seven(w); |
| 85 |
Widget&& rr = seven(std::move(w)); |
| 86 |
} |
| 87 |
|
| 88 |
Widget val(); |
| 89 |
Widget& lref(); |
| 90 |
Widget&& rref(); |
| 91 |
|
| 92 |
decltype(auto) eight() { |
| 93 |
decltype(auto) x = val(); // OK, x is Widget |
| 94 |
return x; // OK, return type is Widget, we get copy elision |
| 95 |
} |
| 96 |
|
| 97 |
decltype(auto) nine() { |
| 98 |
decltype(auto) x = lref(); // OK, x is Widget& |
| 99 |
return x; // OK, return type is Widget& |
| 100 |
} |
| 101 |
|
| 102 |
decltype(auto) ten() { |
| 103 |
decltype(auto) x = rref(); // OK, x is Widget&& |
| 104 |
// This was an error: return type is Widget&&, cannot bind to x. |
| 105 |
// But in C++23, x is treated as an rvalue. |
| 106 |
return x; |
| 107 |
} |
| 108 |
|
| 109 |
// Now returns Widget&&, not Widget&. |
| 110 |
// This is from $ 3.2.1. Interaction with decltype and decltype(auto). |
| 111 |
decltype(auto) eleven(Widget&& x) { |
| 112 |
return (x); |
| 113 |
} |
| 114 |
static_assert(same_type<decltype(eleven), Widget&& (Widget&&)>::value); |