|
| 1 | + |
| 2 | +#define CPP2_USE_MODULES Yes |
| 3 | + |
| 4 | +//=== Cpp2 type declarations ==================================================== |
| 5 | + |
| 6 | + |
| 7 | +#include "cpp2util.h" |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +//=== Cpp2 type definitions and function declarations =========================== |
| 12 | + |
| 13 | +#line 1 "pure2-function-type-id.cpp2" |
| 14 | +auto main() -> int; |
| 15 | + |
| 16 | + |
| 17 | +#line 78 "pure2-function-type-id.cpp2" |
| 18 | +// As non-local function parameter. |
| 19 | +auto g(auto (* f)() -> void) -> void; |
| 20 | +auto g(auto (* f)() -> auto (*)() -> void) -> void; |
| 21 | +// As template parameter. |
| 22 | +template<auto (* V)() -> void> auto g() -> void; |
| 23 | +template<auto (* V)() -> auto (*)() -> void> auto g() -> void; |
| 24 | + |
| 25 | +// In non-local function return type. |
| 26 | +[[nodiscard]] auto g1() -> auto (*)() -> void; |
| 27 | +[[nodiscard]] auto g2() -> auto (*)() -> auto (*)() -> void; |
| 28 | + |
| 29 | +// clang-format off |
| 30 | +// Test case from #343. |
| 31 | +[[nodiscard]] auto f2() -> std::function<auto (cpp2::in<std::string>) -> std::string>; |
| 32 | + |
| 33 | + |
| 34 | +#line 95 "pure2-function-type-id.cpp2" |
| 35 | +// Adapted from <https://github.com/hsutter/cppfront/wiki/Design-note%3A-Postfix-operators>. |
| 36 | + [[nodiscard]] auto f(cpp2::in<cpp2::i32> x) -> auto (*)(cpp2::in<cpp2::i32>) -> std::string; |
| 37 | +auto postfix_operators() -> void; |
| 38 | + |
| 39 | + |
| 40 | +//=== Cpp2 function definitions ================================================= |
| 41 | + |
| 42 | +#line 1 "pure2-function-type-id.cpp2" |
| 43 | +auto main() -> int{ |
| 44 | + postfix_operators(); |
| 45 | + |
| 46 | + // Variables with type of a mix of `*`/`const` to `() throws -> void`. |
| 47 | + std::type_identity_t<auto (*)() -> void> f0 {[]() -> void{}}; |
| 48 | + std::type_identity_t<auto (*)() -> void> const f1 {f0}; |
| 49 | + std::type_identity_t<auto (* const)() -> void> f2 {f0}; |
| 50 | + std::type_identity_t<auto (* const)() -> void> const f3 {f0}; |
| 51 | + |
| 52 | + // Uninitialized. |
| 53 | + cpp2::deferred_init<std::type_identity_t<auto (*)() -> void>> f4; |
| 54 | + f4.construct(f0); |
| 55 | + |
| 56 | + std::type_identity_t<auto (**)() -> void> f10 {&f0}; |
| 57 | + std::type_identity_t<auto (**)() -> void> const f11 {f10}; |
| 58 | + std::type_identity_t<auto (* const*)() -> void> f12 {f10}; |
| 59 | + std::type_identity_t<auto (* const*)() -> void> const f13 {f10}; |
| 60 | + |
| 61 | + cpp2::i32 i {0}; |
| 62 | + cpp2::i32* i0 {&i}; |
| 63 | + cpp2::i32* const i1 {i0}; |
| 64 | + cpp2::i32 const* i2 {i0}; |
| 65 | + cpp2::i32 const* const i3 {i0}; |
| 66 | + |
| 67 | + // Assert consistent '*'/'const' with non-function type variables. |
| 68 | + static_assert((std::is_const_v<decltype(f10)>)==std::is_const_v<decltype(i0)>); |
| 69 | + static_assert((std::is_const_v<decltype(f11)>)==std::is_const_v<decltype(i1)>); |
| 70 | + static_assert((std::is_const_v<decltype(f12)>)==std::is_const_v<decltype(i2)>); |
| 71 | + static_assert((std::is_const_v<decltype(f13)>)==std::is_const_v<decltype(i3)>); |
| 72 | + (void) std::move(f10); |
| 73 | + (void) std::move(f11); |
| 74 | + (void) std::move(f12); |
| 75 | + (void) std::move(f13); |
| 76 | + (void) std::move(i0); |
| 77 | + (void) std::move(i1); |
| 78 | + (void) std::move(i2); |
| 79 | + (void) std::move(i3); |
| 80 | + |
| 81 | + // Variables with various kinds of parameter. |
| 82 | + std::type_identity_t<auto (*)(cpp2::in<cpp2::i32>) -> void> f5 {[](cpp2::in<cpp2::i32> x) -> void{}}; |
| 83 | + std::type_identity_t<auto (*)(cpp2::in<std::any>) -> void> f6 {[](cpp2::in<std::any> x) -> void{}}; |
| 84 | + std::type_identity_t<auto (*)(cpp2::i32&&) -> void> f7 {[](cpp2::i32&& x) -> void{}}; |
| 85 | + std::type_identity_t<auto (*)(cpp2::out<cpp2::i32>) -> void> f8 {[](auto x) -> void{}}; |
| 86 | + |
| 87 | + // In alternative. |
| 88 | + cpp2::Default.expects([&] () -> bool { auto&& __expr = f0; |
| 89 | + if (cpp2::is<auto () noexcept -> void>(__expr)) { if constexpr( requires{std::terminate(), false;} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF((std::terminate(), false)),bool> ) return std::terminate(), false; else return bool{}; else return bool{}; } |
| 90 | + else if (cpp2::is<auto () -> void>(__expr)) { if constexpr( requires{std::terminate(), false;} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF((std::terminate(), false)),bool> ) return std::terminate(), false; else return bool{}; else return bool{}; } |
| 91 | + else if (cpp2::is<auto (*)() -> void>(__expr)) { if constexpr( requires{true;} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF((true)),bool> ) return true; else return bool{}; else return bool{}; } |
| 92 | + else return false; } |
| 93 | + (), ""); |
| 94 | + cpp2::Default.expects([&] () -> bool { auto&& __expr = *cpp2::assert_not_null(f0); |
| 95 | + if (cpp2::is<auto () -> void>(__expr)) { if constexpr( requires{true;} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF((true)),bool> ) return true; else return bool{}; else return bool{}; } |
| 96 | + else return false; } |
| 97 | + (), ""); |
| 98 | +{ |
| 99 | +auto (* f)() -> void = f0; |
| 100 | + |
| 101 | + // As block variable. |
| 102 | +#line 58 "pure2-function-type-id.cpp2" |
| 103 | + {} |
| 104 | +} |
| 105 | +{ |
| 106 | +cpp2::in<auto () -> void> f = *cpp2::assert_not_null(f0); |
| 107 | +#line 59 "pure2-function-type-id.cpp2" |
| 108 | + { } |
| 109 | +} |
| 110 | + |
| 111 | + // As local function parameter. |
| 112 | +#line 62 "pure2-function-type-id.cpp2" |
| 113 | + (void) [](auto (* f)() -> void) -> void{}; |
| 114 | + (void) [](auto (* f)() -> auto (*)() -> void) -> void{}; |
| 115 | + (void) [](auto (* f)() -> auto (*)() -> auto (*)() -> void) -> void{}; |
| 116 | + |
| 117 | + // In local function return type. |
| 118 | + (void) []() -> auto (*)() -> void { return nullptr; }; |
| 119 | + (void) []() -> auto (*)() -> auto (*)() -> void { return nullptr; }; |
| 120 | + |
| 121 | + // Without `throws`. |
| 122 | + (void) std::type_identity_t<auto (*)(std::string_view, CPP2_MESSAGE_PARAM) noexcept -> void>{cpp2::report_and_terminate}; |
| 123 | + |
| 124 | + // As template argument. |
| 125 | + (void) std::type_identity_t<auto (*)() -> void>{std::move(f0)}; |
| 126 | + static_assert(std::is_function_v<auto () -> void>); |
| 127 | +} |
| 128 | + |
| 129 | +#line 79 "pure2-function-type-id.cpp2" |
| 130 | +auto g(auto (* f)() -> void) -> void{} |
| 131 | +auto g(auto (* f)() -> auto (*)() -> void) -> void{} |
| 132 | + |
| 133 | +template<auto (* V)() -> void> auto g() -> void{} |
| 134 | +template<auto (* V)() -> auto (*)() -> void> auto g() -> void{} |
| 135 | + |
| 136 | +#line 86 "pure2-function-type-id.cpp2" |
| 137 | +[[nodiscard]] auto g1() -> auto (*)() -> void { return nullptr; } |
| 138 | +[[nodiscard]] auto g2() -> auto (*)() -> auto (*)() -> void { return nullptr; } |
| 139 | + |
| 140 | +#line 91 "pure2-function-type-id.cpp2" |
| 141 | +[[nodiscard]] auto f2() -> std::function<auto (cpp2::in<std::string>) -> std::string>{ |
| 142 | + return [](cpp2::in<std::string> s) -> std::string{return s + " World!"; }; |
| 143 | +} |
| 144 | + |
| 145 | +#line 96 "pure2-function-type-id.cpp2" |
| 146 | + [[nodiscard]] auto f(cpp2::in<cpp2::i32> x) -> auto (*)(cpp2::in<cpp2::i32>) -> std::string { return +[](cpp2::in<cpp2::i32> x) -> std::string { return ""; }; } |
| 147 | +auto postfix_operators() -> void{ |
| 148 | + cpp2::Default.expects(cpp2::is<auto (cpp2::in<cpp2::i32>) -> auto (*)(cpp2::in<cpp2::i32>) -> std::string>(f), ""); |
| 149 | + // / | | |
| 150 | + // / | | |
| 151 | + cpp2::Default.expects(cpp2::is<auto (*)(cpp2::in<cpp2::i32>) -> std::string>(f(42)), ""); |
| 152 | + // ________________/ | |
| 153 | + // / | |
| 154 | + cpp2::Default.expects(cpp2::is<auto (cpp2::in<cpp2::i32>) -> std::string>(*cpp2::assert_not_null(f(42))), ""); |
| 155 | + // _______________/ |
| 156 | + // / |
| 157 | + cpp2::Default.expects(cpp2::is<std::string>((*cpp2::assert_not_null(f(42)))(1)), ""); |
| 158 | +} // clang-format on |
| 159 | + |
0 commit comments