1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
12 // constexpr explicit(!is_convertible_v<G, E>) expected(unexpected<G>&& e);
16 // Constraints: is_constructible_v<E, GF> is true.
18 // Effects: Direct-non-list-initializes unex with std::forward<GF>(e.error()).
20 // Postconditions: has_value() is false.
22 // Throws: Any exception thrown by the initialization of unex.
26 #include <type_traits>
30 #include "test_macros.h"
31 #include "../../types.h"
34 static_assert(std::is_constructible_v
<std::expected
<void, int>, std::unexpected
<int>>);
35 static_assert(std::is_constructible_v
<std::expected
<void, MoveOnly
>, std::unexpected
<MoveOnly
>>);
37 // !is_constructible_v<E, GF>
39 static_assert(!std::is_constructible_v
<std::expected
<void, int>, std::unexpected
<foo
>>);
41 // explicit(!is_convertible_v<G, E>)
42 struct NotConvertible
{
43 explicit NotConvertible(int);
45 static_assert(std::is_convertible_v
<std::unexpected
<int>&&, std::expected
<void, int>>);
46 static_assert(!std::is_convertible_v
<std::unexpected
<int>&&, std::expected
<void, NotConvertible
>>);
50 constexpr MyInt(int ii
) : i(ii
) {}
51 friend constexpr bool operator==(const MyInt
&, const MyInt
&) = default;
55 constexpr void testInt() {
56 std::unexpected
<int> u(5);
57 std::expected
<void, Err
> e(std::move(u
));
58 assert(!e
.has_value());
59 assert(e
.error() == 5);
62 constexpr void testMoveOnly() {
63 std::unexpected
<MoveOnly
> u(MoveOnly(5));
64 std::expected
<void, MoveOnly
> e(std::move(u
));
65 assert(!e
.has_value());
66 assert(e
.error() == 5);
67 assert(u
.error() == 0);
70 constexpr bool test() {
74 testInt
<TailClobberer
<1>>();
79 void testException() {
80 #ifndef TEST_HAS_NO_EXCEPTIONS
82 Throwing(int) { throw Except
{}; }
86 std::unexpected
<int> u(5);
88 [[maybe_unused
]] std::expected
<void, Throwing
> e(std::move(u
));
94 #endif // TEST_HAS_NO_EXCEPTIONS
97 int main(int, char**) {
99 static_assert(test());