//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 // template // constexpr explicit(!is_convertible_v) expected(const unexpected& e); // // Let GF be const G& // // Constraints: is_constructible_v is true. // // Effects: Direct-non-list-initializes unex with std::forward(e.error()). // // Postconditions: has_value() is false. // // Throws: Any exception thrown by the initialization of unex. #include #include #include #include #include "MoveOnly.h" #include "test_macros.h" #include "../../types.h" // Test Constraints static_assert(std::is_constructible_v, const std::unexpected&>); // !is_constructible_v struct foo {}; static_assert(!std::is_constructible_v, const std::unexpected&>); static_assert(!std::is_constructible_v, const std::unexpected&>); // explicit(!is_convertible_v) struct NotConvertible { explicit NotConvertible(int); }; static_assert(std::is_convertible_v&, std::expected>); static_assert(!std::is_convertible_v&, std::expected>); struct MyInt { int i; constexpr MyInt(int ii) : i(ii) {} friend constexpr bool operator==(const MyInt&, const MyInt&) = default; }; template constexpr void testUnexpected() { const std::unexpected u(5); std::expected e(u); assert(!e.has_value()); assert(e.error() == 5); } constexpr bool test() { testUnexpected(); testUnexpected(); testUnexpected, bool>(); return true; } void testException() { #ifndef TEST_HAS_NO_EXCEPTIONS struct Throwing { Throwing(int) { throw Except{}; } }; { const std::unexpected u(5); try { [[maybe_unused]] std::expected e(u); assert(false); } catch (Except) { } } #endif // TEST_HAS_NO_EXCEPTIONS } int main(int, char**) { test(); static_assert(test()); testException(); return 0; }