|
| 1 | + |
| 2 | + |
| 3 | +//=== Cpp2 type declarations ==================================================== |
| 4 | + |
| 5 | + |
| 6 | +#include "cpp2util.h" |
| 7 | + |
| 8 | +#line 1 "mixed-as-with-typesafety.cpp2" |
| 9 | + |
| 10 | + |
| 11 | +//=== Cpp2 type definitions and function declarations =========================== |
| 12 | + |
| 13 | +#line 1 "mixed-as-with-typesafety.cpp2" |
| 14 | +void throw_error(CPP2_MESSAGE_PARAM msg CPP2_SOURCE_LOCATION_PARAM) { |
| 15 | + throw std::runtime_error(std::string("Type safety exception: ") + msg); |
| 16 | +} |
| 17 | + |
| 18 | +#line 5 "mixed-as-with-typesafety.cpp2" |
| 19 | +[[nodiscard]] auto make_throwable(cpp2::contract_group& cg) -> auto; |
| 20 | +#line 13 "mixed-as-with-typesafety.cpp2" |
| 21 | + |
| 22 | +void expect_no_throw(auto&& fun) try { |
| 23 | + fun(); |
| 24 | +} catch(std::exception const& e) { |
| 25 | + std::cout << e.what() << std::endl; |
| 26 | +} catch(...) { |
| 27 | + std::cout << "Unknown exception!" << std::endl; |
| 28 | +} |
| 29 | + |
| 30 | +auto main() -> int; |
| 31 | + |
| 32 | +//=== Cpp2 function definitions ================================================= |
| 33 | + |
| 34 | +#line 1 "mixed-as-with-typesafety.cpp2" |
| 35 | + |
| 36 | +#line 5 "mixed-as-with-typesafety.cpp2" |
| 37 | +[[nodiscard]] auto make_throwable(cpp2::contract_group& cg) -> auto{ |
| 38 | + auto h {CPP2_UFCS(get_handler)(cg)}; |
| 39 | + auto sh {[_0 = cpp2::move(h)](cpp2::contract_group* pcg) mutable -> void{ |
| 40 | + CPP2_UFCS(set_handler)((*cpp2::impl::assert_not_null(pcg)), _0); |
| 41 | + }}; |
| 42 | + CPP2_UFCS(set_handler)(cg, throw_error); |
| 43 | + return std::unique_ptr<cpp2::contract_group,decltype(sh)>(&cg, cpp2::move(sh)); |
| 44 | +} |
| 45 | + |
| 46 | +#line 22 "mixed-as-with-typesafety.cpp2" |
| 47 | +auto main() -> int{ |
| 48 | + std::optional<int> o {}; |
| 49 | +{ |
| 50 | +[[maybe_unused]] auto const& unnamed_param_1{make_throwable(cpp2::type_safety)}; |
| 51 | + |
| 52 | +#line 25 "mixed-as-with-typesafety.cpp2" |
| 53 | + { |
| 54 | + expect_no_throw([_0 = o]() mutable -> void{ |
| 55 | + std::cout << (cpp2::impl::as_<int>(_0)) << std::endl;// that will throw |
| 56 | + }); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#line 31 "mixed-as-with-typesafety.cpp2" |
| 61 | + std::cout << (cpp2::impl::as_<int>(cpp2::move(o))) << std::endl;// that will terminate |
| 62 | +} |
| 63 | + |
0 commit comments