//===----------------------------------------------------------------------===// // 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(unexpected&& e); // // Let GF be 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" // Test Constraints static_assert(std::is_constructible_v, std::unexpected>); static_assert(std::is_constructible_v, std::unexpected>); // !is_constructible_v struct foo {}; static_assert(!std::is_constructible_v, 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 testInt() { std::unexpected u(5); std::expected e(std::move(u)); assert(!e.has_value()); assert(e.error() == 5); } constexpr void testMoveOnly() { std::unexpected u(MoveOnly(5)); std::expected e(std::move(u)); assert(!e.has_value()); assert(e.error() == 5); assert(u.error() == 0); } constexpr bool test() { testInt(); testInt(); testInt(); testMoveOnly(); return true; } void testException() { #ifndef TEST_HAS_NO_EXCEPTIONS struct Except {}; struct Throwing { Throwing(int) { throw Except{}; } }; { std::unexpected u(5); try { [[maybe_unused]] std::expected e(std::move(u)); assert(false); } catch (Except) { } } #endif // TEST_HAS_NO_EXCEPTIONS } int main(int, char**) { test(); static_assert(test()); testException(); return 0; }