| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 | |
| 11 | // template<class... Args> |
| 12 | // constexpr explicit expected(in_place_t, Args&&... args); |
| 13 | // |
| 14 | // Constraints: is_constructible_v<T, Args...> is true. |
| 15 | // |
| 16 | // Effects: Direct-non-list-initializes val with std::forward<Args>(args).... |
| 17 | // |
| 18 | // Postconditions: has_value() is true. |
| 19 | // |
| 20 | // Throws: Any exception thrown by the initialization of val. |
| 21 | |
| 22 | #include <cassert> |
| 23 | #include <expected> |
| 24 | #include <type_traits> |
| 25 | #include <tuple> |
| 26 | #include <utility> |
| 27 | |
| 28 | #include "MoveOnly.h" |
| 29 | #include "test_macros.h" |
| 30 | #include "../../types.h" |
| 31 | |
| 32 | // Test Constraints: |
| 33 | static_assert(std::is_constructible_v<std::expected<int, int>, std::in_place_t>); |
| 34 | static_assert(std::is_constructible_v<std::expected<int, int>, std::in_place_t, int>); |
| 35 | |
| 36 | // !is_constructible_v<T, Args...> |
| 37 | struct foo {}; |
| 38 | static_assert(!std::is_constructible_v<std::expected<foo, int>, std::in_place_t, int>); |
| 39 | |
| 40 | // test explicit |
| 41 | template <class T> |
| 42 | void conversion_test(T); |
| 43 | |
| 44 | template <class T, class... Args> |
| 45 | concept ImplicitlyConstructible = requires(Args&&... args) { conversion_test<T>({std::forward<Args>(args)...}); }; |
| 46 | static_assert(ImplicitlyConstructible<int, int>); |
| 47 | |
| 48 | static_assert(!ImplicitlyConstructible<std::expected<int, int>, std::in_place_t>); |
| 49 | static_assert(!ImplicitlyConstructible<std::expected<int, int>, std::in_place_t, int>); |
| 50 | |
| 51 | struct CopyOnly { |
| 52 | int i; |
| 53 | constexpr CopyOnly(int ii) : i(ii) {} |
| 54 | CopyOnly(const CopyOnly&) = default; |
| 55 | CopyOnly(CopyOnly&&) = delete; |
| 56 | friend constexpr bool operator==(const CopyOnly& mi, int ii) { return mi.i == ii; } |
| 57 | }; |
| 58 | |
| 59 | template <class T, class E = int> |
| 60 | constexpr void testInt() { |
| 61 | std::expected<T, E> e(std::in_place, 5); |
| 62 | assert(e.has_value()); |
| 63 | assert(e.value() == 5); |
| 64 | } |
| 65 | |
| 66 | template <class T, class E = int> |
| 67 | constexpr void testLValue() { |
| 68 | T t(5); |
| 69 | std::expected<T, E> e(std::in_place, t); |
| 70 | assert(e.has_value()); |
| 71 | assert(e.value() == 5); |
| 72 | } |
| 73 | |
| 74 | template <class T, class E = int> |
| 75 | constexpr void testRValue() { |
| 76 | std::expected<T, E> e(std::in_place, T(5)); |
| 77 | assert(e.has_value()); |
| 78 | assert(e.value() == 5); |
| 79 | } |
| 80 | |
| 81 | constexpr bool test() { |
| 82 | testInt<int>(); |
| 83 | testInt<CopyOnly>(); |
| 84 | testInt<MoveOnly>(); |
| 85 | testInt<TailClobberer<0>, bool>(); |
| 86 | testLValue<int>(); |
| 87 | testLValue<CopyOnly>(); |
| 88 | testLValue<TailClobberer<0>, bool>(); |
| 89 | testRValue<int>(); |
| 90 | testRValue<MoveOnly>(); |
| 91 | testRValue<TailClobberer<0>, bool>(); |
| 92 | |
| 93 | // no arg |
| 94 | { |
| 95 | std::expected<int, int> e(std::in_place); |
| 96 | assert(e.has_value()); |
| 97 | assert(e.value() == 0); |
| 98 | } |
| 99 | |
| 100 | // one arg |
| 101 | { |
| 102 | std::expected<int, int> e(std::in_place, 5); |
| 103 | assert(e.has_value()); |
| 104 | assert(e.value() == 5); |
| 105 | } |
| 106 | |
| 107 | // multi args |
| 108 | { |
| 109 | std::expected<std::tuple<int, short, MoveOnly>, int> e(std::in_place, 1, short{2}, MoveOnly(3)); |
| 110 | assert(e.has_value()); |
| 111 | assert((e.value() == std::tuple<int, short, MoveOnly>(1, short{2}, MoveOnly(3)))); |
| 112 | } |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | void testException() { |
| 118 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 119 | struct Throwing { |
| 120 | Throwing(int) { throw Except{}; }; |
| 121 | }; |
| 122 | |
| 123 | try { |
| 124 | std::expected<Throwing, int> u(std::in_place, 5); |
| 125 | assert(false); |
| 126 | } catch (Except) { |
| 127 | } |
| 128 | #endif // TEST_HAS_NO_EXCEPTIONS |
| 129 | } |
| 130 | |
| 131 | int main(int, char**) { |
| 132 | test(); |
| 133 | static_assert(test()); |
| 134 | testException(); |
| 135 | return 0; |
| 136 | } |
| 137 | |