| 1 | // { dg-do compile { target c++23 } } |
| 2 | // { dg-add-options no_pch } |
| 3 | |
| 4 | #include <functional> |
| 5 | |
| 6 | #ifndef __cpp_lib_invoke_r |
| 7 | # error Feature-test macro for invoke_r is missing in <functional> |
| 8 | #elif __cpp_lib_invoke_r < 202106L |
| 9 | # error Feature-test macro for invoke_r has the wrong value in <functional> |
| 10 | #endif |
| 11 | |
| 12 | constexpr int sq(int i) { return i * i; } |
| 13 | |
| 14 | template<typename Val, typename Expected> |
| 15 | constexpr bool chk(Val&& val, Expected&& exp) |
| 16 | { |
| 17 | return std::is_same_v<Val, Expected> && val == exp; |
| 18 | } |
| 19 | |
| 20 | void |
| 21 | test01() |
| 22 | { |
| 23 | static_assert( chk( std::invoke(sq, 2), 4 ) ); |
| 24 | static_assert( chk( std::invoke_r<int>(sq, 3), 9 ) ); |
| 25 | static_assert( chk( std::invoke_r<char>(sq, 4), '\x10' ) ); |
| 26 | } |
| 27 | |
| 28 | struct abstract { |
| 29 | virtual ~abstract() = 0; |
| 30 | void operator()() noexcept; |
| 31 | }; |
| 32 | |
| 33 | static_assert( noexcept(std::invoke(std::declval<abstract>())), |
| 34 | "It should be possible to use abstract types with INVOKE" ); |
| 35 | |
| 36 | static_assert( noexcept(std::invoke_r<void>(std::declval<abstract>())), |
| 37 | "It should be possible to use abstract types with INVOKE<R>" ); |
| 38 | |
| 39 | struct F { |
| 40 | void operator()() &; |
| 41 | void operator()() && noexcept; |
| 42 | int operator()(int); |
| 43 | double* operator()(int, int) noexcept; |
| 44 | }; |
| 45 | struct D { D(void*); }; |
| 46 | |
| 47 | static_assert( !noexcept(std::invoke(std::declval<F&>())) ); |
| 48 | static_assert( noexcept(std::invoke(std::declval<F>())) ); |
| 49 | static_assert( !noexcept(std::invoke(std::declval<F>(), 1)) ); |
| 50 | static_assert( noexcept(std::invoke(std::declval<F>(), 1, 2)) ); |
| 51 | |
| 52 | static_assert( !noexcept(std::invoke_r<void>(std::declval<F&>())) ); |
| 53 | static_assert( noexcept(std::invoke_r<void>(std::declval<F>())) ); |
| 54 | static_assert( !noexcept(std::invoke_r<int>(std::declval<F>(), 1)) ); |
| 55 | static_assert( !noexcept(std::invoke_r<void>(std::declval<F>(), 1)) ); |
| 56 | static_assert( !noexcept(std::invoke_r<long>(std::declval<F>(), 1)) ); |
| 57 | static_assert( noexcept(std::invoke_r<void>(std::declval<F>(), 1, 2)) ); |
| 58 | static_assert( noexcept(std::invoke_r<void*>(std::declval<F>(), 1, 2)) ); |
| 59 | static_assert( !noexcept(std::invoke_r<D>(std::declval<F>(), 1, 2)) ); |