| 1 |
// { dg-do compile { target c++23 } } |
| 2 |
// { dg-require-effective-target hosted } |
| 3 |
// { dg-add-options no_pch } |
| 4 |
|
| 5 |
#include <functional> |
| 6 |
|
| 7 |
#ifndef __cpp_lib_move_only_function |
| 8 |
# error "Feature-test macro for move_only_function missing in <functional>" |
| 9 |
#elif __cpp_lib_move_only_function != 202110L |
| 10 |
# error "Feature-test macro for move_only_function has wrong value in <functional>" |
| 11 |
#endif |
| 12 |
|
| 13 |
using std::move_only_function; |
| 14 |
|
| 15 |
using std::is_constructible_v; |
| 16 |
using std::is_copy_constructible_v; |
| 17 |
using std::is_nothrow_default_constructible_v; |
| 18 |
using std::is_nothrow_move_constructible_v; |
| 19 |
using std::is_nothrow_constructible_v; |
| 20 |
using std::nullptr_t; |
| 21 |
using std::in_place_type_t; |
| 22 |
|
| 23 |
static_assert( is_nothrow_default_constructible_v<move_only_function<void()>> ); |
| 24 |
static_assert( is_nothrow_constructible_v<move_only_function<void()>, nullptr_t> ); |
| 25 |
static_assert( is_nothrow_move_constructible_v<move_only_function<void()>> ); |
| 26 |
static_assert( ! is_copy_constructible_v<move_only_function<void()>> ); |
| 27 |
|
| 28 |
static_assert( is_constructible_v<move_only_function<void()>, void()> ); |
| 29 |
static_assert( is_constructible_v<move_only_function<void()>, void(&)()> ); |
| 30 |
static_assert( is_constructible_v<move_only_function<void()>, void(*)()> ); |
| 31 |
static_assert( is_constructible_v<move_only_function<void()>, int()> ); |
| 32 |
static_assert( is_constructible_v<move_only_function<void()>, int(&)()> ); |
| 33 |
static_assert( is_constructible_v<move_only_function<void()>, int(*)()> ); |
| 34 |
static_assert( ! is_constructible_v<move_only_function<void()>, void(int)> ); |
| 35 |
static_assert( is_constructible_v<move_only_function<void(int)>, void(int)> ); |
| 36 |
|
| 37 |
static_assert( is_constructible_v<move_only_function<void(int)>, |
| 38 |
in_place_type_t<void(*)(int)>, void(int)> ); |
| 39 |
|
| 40 |
static_assert( is_constructible_v<move_only_function<void()>, |
| 41 |
void() noexcept> ); |
| 42 |
static_assert( is_constructible_v<move_only_function<void() noexcept>, |
| 43 |
void() noexcept> ); |
| 44 |
static_assert( ! is_constructible_v<move_only_function<void() noexcept>, |
| 45 |
void() > ); |
| 46 |
|
| 47 |
struct Q |
| 48 |
{ |
| 49 |
void operator()() const &; |
| 50 |
void operator()() &&; |
| 51 |
}; |
| 52 |
|
| 53 |
static_assert( is_constructible_v<move_only_function<void()>, Q> ); |
| 54 |
static_assert( is_constructible_v<move_only_function<void() const>, Q> ); |
| 55 |
static_assert( is_constructible_v<move_only_function<void() &>, Q> ); |
| 56 |
static_assert( is_constructible_v<move_only_function<void() const &>, Q> ); |
| 57 |
static_assert( is_constructible_v<move_only_function<void() &&>, Q> ); |
| 58 |
static_assert( is_constructible_v<move_only_function<void() const &&>, Q> ); |
| 59 |
|
| 60 |
struct R |
| 61 |
{ |
| 62 |
void operator()() &; |
| 63 |
void operator()() &&; |
| 64 |
}; |
| 65 |
|
| 66 |
static_assert( is_constructible_v<move_only_function<void()>, R> ); |
| 67 |
static_assert( is_constructible_v<move_only_function<void()&>, R> ); |
| 68 |
static_assert( is_constructible_v<move_only_function<void()&&>, R> ); |
| 69 |
static_assert( ! is_constructible_v<move_only_function<void() const>, R> ); |
| 70 |
static_assert( ! is_constructible_v<move_only_function<void() const&>, R> ); |
| 71 |
static_assert( ! is_constructible_v<move_only_function<void() const&&>, R> ); |
| 72 |
|
| 73 |
// The following nothrow-constructible guarantees are a GCC extension, |
| 74 |
// not required by the standard. |
| 75 |
|
| 76 |
static_assert( is_nothrow_constructible_v<move_only_function<void()>, void()> ); |
| 77 |
static_assert( is_nothrow_constructible_v<move_only_function<void(int)>, |
| 78 |
in_place_type_t<void(*)(int)>, |
| 79 |
void(int)> ); |
| 80 |
|
| 81 |
// These types are all small and nothrow move constructible |
| 82 |
struct F { void operator()(); }; |
| 83 |
struct G { void operator()() const; }; |
| 84 |
static_assert( is_nothrow_constructible_v<move_only_function<void()>, F> ); |
| 85 |
static_assert( is_nothrow_constructible_v<move_only_function<void()>, G> ); |
| 86 |
static_assert( is_nothrow_constructible_v<move_only_function<void() const>, G> ); |
| 87 |
|
| 88 |
struct H { |
| 89 |
H(int); |
| 90 |
H(int, int) noexcept; |
| 91 |
void operator()() noexcept; |
| 92 |
}; |
| 93 |
static_assert( is_nothrow_constructible_v<move_only_function<void()>, H> ); |
| 94 |
static_assert( is_nothrow_constructible_v<move_only_function<void() noexcept>, |
| 95 |
H> ); |
| 96 |
static_assert( ! is_nothrow_constructible_v<move_only_function<void() noexcept>, |
| 97 |
in_place_type_t<H>, int> ); |
| 98 |
static_assert( is_nothrow_constructible_v<move_only_function<void() noexcept>, |
| 99 |
in_place_type_t<H>, int, int> ); |
| 100 |
|
| 101 |
struct I { |
| 102 |
I(int, const char*); |
| 103 |
I(std::initializer_list<char>); |
| 104 |
int operator()() const noexcept; |
| 105 |
}; |
| 106 |
|
| 107 |
static_assert( is_constructible_v<move_only_function<void()>, |
| 108 |
std::in_place_type_t<I>, |
| 109 |
int, const char*> ); |
| 110 |
static_assert( is_constructible_v<move_only_function<void()>, |
| 111 |
std::in_place_type_t<I>, |
| 112 |
std::initializer_list<char>> ); |
| 113 |
|
| 114 |
void |
| 115 |
test_instantiation() |
| 116 |
{ |
| 117 |
// Instantiate the constructor bodies |
| 118 |
move_only_function<void()> f0; |
| 119 |
move_only_function<void()> f1(nullptr); |
| 120 |
move_only_function<void()> f2( I(1, "two") ); |
| 121 |
move_only_function<void()> f3(std::in_place_type<I>, 3, "four"); |
| 122 |
move_only_function<void()> f4(std::in_place_type<I>, // PR libstdc++/102825 |
| 123 |
{ 'P', 'R', '1', '0', '2', '8', '2', '5'}); |
| 124 |
auto f5 = std::move(f4); |
| 125 |
f4 = std::move(f5); |
| 126 |
} |