1//===----------------------------------------------------------------------===//
2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3// See https://llvm.org/LICENSE.txt for license information.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7
8// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
9
10// constexpr expected(const expected& rhs);
11//
12// Effects: If rhs.has_value() is false, direct-non-list-initializes unex with rhs.error().
13//
14// Postconditions: rhs.has_value() == this->has_value().
15//
16// Throws: Any exception thrown by the initialization of unex.
17//
18// Remarks:
19// - This constructor is defined as deleted unless is_copy_constructible_v<E> is true.
20// - This constructor is trivial if is_trivially_copy_constructible_v<E> is true.
21
22#include <cassert>
23#include <expected>
24#include <type_traits>
25#include <utility>
26
27#include "test_macros.h"
28#include "../../types.h"
29
30struct NonCopyable {
31 NonCopyable(const NonCopyable&) = delete;
32};
33
34struct CopyableNonTrivial {
35 int i;
36 constexpr CopyableNonTrivial(int ii) : i(ii) {}
37 constexpr CopyableNonTrivial(const CopyableNonTrivial& o) { i = o.i; }
38 friend constexpr bool operator==(const CopyableNonTrivial&, const CopyableNonTrivial&) = default;
39};
40
41// Test: This constructor is defined as deleted unless is_copy_constructible_v<E> is true.
42static_assert(std::is_copy_constructible_v<std::expected<void, int>>);
43static_assert(std::is_copy_constructible_v<std::expected<void, CopyableNonTrivial>>);
44static_assert(!std::is_copy_constructible_v<std::expected<void, NonCopyable>>);
45
46// Test: This constructor is trivial if is_trivially_copy_constructible_v<E> is true.
47static_assert(std::is_trivially_copy_constructible_v<std::expected<void, int>>);
48static_assert(!std::is_trivially_copy_constructible_v<std::expected<void, CopyableNonTrivial>>);
49
50constexpr bool test() {
51 // copy the error non-trivial
52 {
53 const std::expected<void, CopyableNonTrivial> e1(std::unexpect, 5);
54 auto e2 = e1;
55 assert(!e2.has_value());
56 assert(e2.error().i == 5);
57 }
58
59 // copy the error trivial
60 {
61 const std::expected<void, int> e1(std::unexpect, 5);
62 auto e2 = e1;
63 assert(!e2.has_value());
64 assert(e2.error() == 5);
65 }
66
67 // copy TailClobberer as error
68 {
69 const std::expected<void, TailClobberer<1>> e1(std::unexpect);
70 auto e2 = e1;
71 assert(!e2.has_value());
72 }
73
74 return true;
75}
76
77void testException() {
78#ifndef TEST_HAS_NO_EXCEPTIONS
79 struct Throwing {
80 Throwing() = default;
81 Throwing(const Throwing&) { throw Except{}; }
82 };
83
84 // throw on copying error
85 {
86 const std::expected<void, Throwing> e1(std::unexpect);
87 try {
88 [[maybe_unused]] auto e2 = e1;
89 assert(false);
90 } catch (Except) {
91 }
92 }
93
94#endif // TEST_HAS_NO_EXCEPTIONS
95}
96
97int main(int, char**) {
98 test();
99 static_assert(test());
100 testException();
101 return 0;
102}
103

source code of libcxx/test/std/utilities/expected/expected.void/ctor/ctor.copy.pass.cpp