|
| 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 39 "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 | +// Adapted from <https://github.com/hsutter/cppfront/wiki/Design-note%3A-Postfix-operators>. |
| 26 | +// clang-format off |
| 27 | + [[nodiscard]] auto f(cpp2::in<cpp2::i32> x) -> auto (*)(cpp2::in<cpp2::i32>) -> std::string; |
| 28 | +auto postfix_operators() -> void; |
| 29 | + |
| 30 | + |
| 31 | +//=== Cpp2 function definitions ================================================= |
| 32 | + |
| 33 | +#line 1 "pure2-function-type-id.cpp2" |
| 34 | +auto main() -> int{ |
| 35 | + postfix_operators(); |
| 36 | + |
| 37 | + // Variables with type of a mix of `*`/`const` to `() throws -> void`. |
| 38 | + std::type_identity_t<auto (*)() -> void> f0 {[]() -> void{}}; |
| 39 | + std::type_identity_t<auto (*)() -> void> const f1 {f0}; |
| 40 | + std::type_identity_t<auto (* const)() -> void> f2 {f0}; |
| 41 | + std::type_identity_t<auto (* const)() -> void> const f3 {f0}; |
| 42 | + |
| 43 | + cpp2::i32 i {0}; |
| 44 | + cpp2::i32* i0 {&i}; |
| 45 | + cpp2::i32* const i1 {i0}; |
| 46 | + cpp2::i32 const* i2 {i0}; |
| 47 | + cpp2::i32 const* const i3 {i0}; |
| 48 | + |
| 49 | + // Assert consistent '*'/'const' with non-function type variables. |
| 50 | + static_assert((std::is_const_v<decltype(std::move(f1))>)==std::is_const_v<decltype(std::move(i1))>); |
| 51 | + static_assert((std::is_const_v<decltype(std::move(f2))>)==std::is_const_v<decltype(std::move(i2))>); |
| 52 | + static_assert((std::is_const_v<decltype(std::move(f3))>)==std::is_const_v<decltype(std::move(i3))>); |
| 53 | + |
| 54 | + // Variables with various kinds of parameter. |
| 55 | + std::type_identity_t<auto (*)(cpp2::in<cpp2::i32>) -> void> f4 {[](cpp2::in<cpp2::i32> x) -> void{}}; |
| 56 | + std::type_identity_t<auto (*)(cpp2::in<std::any>) -> void> f5 {[](cpp2::in<std::any> x) -> void{}}; |
| 57 | + std::type_identity_t<auto (*)(cpp2::i32&&) -> void> f6 {[](cpp2::i32&& x) -> void{}}; |
| 58 | + std::type_identity_t<auto (*)(cpp2::out<cpp2::i32>) -> void> f7 {[](auto x) -> void{}}; |
| 59 | + |
| 60 | + // As local function parameter. |
| 61 | + (void) [](auto (* f)() -> void) -> void{}; |
| 62 | + (void) [](auto (* f)() -> auto (*)() -> void) -> void{}; |
| 63 | + (void) [](auto (* f)() -> auto (*)() -> auto (*)() -> void) -> void{}; |
| 64 | + |
| 65 | + // Without `throws`. |
| 66 | + (void) std::type_identity_t<auto (*)(std::string_view, CPP2_MESSAGE_PARAM) noexcept -> void>{cpp2::report_and_terminate}; |
| 67 | + |
| 68 | + // As template argument. |
| 69 | + (void) std::type_identity_t<auto (*)() -> void>{std::move(f0)}; |
| 70 | +} |
| 71 | + |
| 72 | +#line 40 "pure2-function-type-id.cpp2" |
| 73 | +auto g(auto (* f)() -> void) -> void{} |
| 74 | +auto g(auto (* f)() -> auto (*)() -> void) -> void{} |
| 75 | + |
| 76 | +template<auto (* V)() -> void> auto g() -> void{} |
| 77 | +template<auto (* V)() -> auto (*)() -> void> auto g() -> void{} |
| 78 | + |
| 79 | +#line 48 "pure2-function-type-id.cpp2" |
| 80 | + [[nodiscard]] auto f(cpp2::in<cpp2::i32> x) -> auto (*)(cpp2::in<cpp2::i32>) -> std::string { return +[](cpp2::in<cpp2::i32> x) -> std::string { return ""; }; } |
| 81 | +auto postfix_operators() -> void{ |
| 82 | + cpp2::Default.expects(cpp2::is<auto (cpp2::in<cpp2::i32>) -> auto (*)(cpp2::in<cpp2::i32>) -> std::string>(f), ""); |
| 83 | + // / | | |
| 84 | + // / | | |
| 85 | + cpp2::Default.expects(cpp2::is<auto (*)(cpp2::in<cpp2::i32>) -> std::string>(f(42)), ""); |
| 86 | + // ________________/ | |
| 87 | + // / | |
| 88 | + cpp2::Default.expects(cpp2::is<auto (cpp2::in<cpp2::i32>) -> std::string>(*cpp2::assert_not_null(f(42))), ""); |
| 89 | + // _______________/ |
| 90 | + // / |
| 91 | + cpp2::Default.expects(cpp2::is<std::string>((*cpp2::assert_not_null(f(42)))(1)), ""); |
| 92 | +} // clang-format on |
| 93 | + |
0 commit comments