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
10 // <optional>
11
12 // template <class U>
13 // optional(const optional<U>& rhs);
14
15 #include <optional>
16 #include <type_traits>
17 #include <cassert>
18
19 #include "test_macros.h"
20
21 using std::optional;
22
23 template <class T, class U>
24 TEST_CONSTEXPR_CXX20 void
test(const optional<U> & rhs,bool is_going_to_throw=false)25 test(const optional<U>& rhs, bool is_going_to_throw = false)
26 {
27 bool rhs_engaged = static_cast<bool>(rhs);
28 #ifndef TEST_HAS_NO_EXCEPTIONS
29 try
30 {
31 optional<T> lhs = rhs;
32 assert(is_going_to_throw == false);
33 assert(static_cast<bool>(lhs) == rhs_engaged);
34 if (rhs_engaged)
35 assert(*lhs == *rhs);
36 }
37 catch (int i)
38 {
39 assert(i == 6);
40 }
41 #else
42 if (is_going_to_throw) return;
43 optional<T> lhs = rhs;
44 assert(static_cast<bool>(lhs) == rhs_engaged);
45 if (rhs_engaged)
46 assert(*lhs == *rhs);
47 #endif
48 }
49
50 class X
51 {
52 int i_;
53 public:
X(int i)54 constexpr X(int i) : i_(i) {}
X(const X & x)55 constexpr X(const X& x) : i_(x.i_) {}
~X()56 TEST_CONSTEXPR_CXX20 ~X() {i_ = 0;}
operator ==(const X & x,const X & y)57 friend constexpr bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
58 };
59
60 class Y
61 {
62 int i_;
63 public:
Y(int i)64 constexpr Y(int i) : i_(i) {}
65
operator ==(const Y & x,const Y & y)66 friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;}
67 };
68
69 int count = 0;
70
71 class Z
72 {
73 int i_;
74 public:
Z(int i)75 Z(int i) : i_(i) {TEST_THROW(6);}
76
operator ==(const Z & x,const Z & y)77 friend bool operator==(const Z& x, const Z& y) {return x.i_ == y.i_;}
78 };
79
80 template<class T, class U>
test_all()81 constexpr bool test_all()
82 {
83 {
84 optional<U> rhs;
85 test<T>(rhs);
86 }
87 {
88 optional<U> rhs(U{3});
89 test<T>(rhs);
90 }
91 return true;
92 }
93
main(int,char **)94 int main(int, char**)
95 {
96 test_all<int, short>();
97 test_all<X, int>();
98 test_all<Y, int>();
99 #if TEST_STD_VER > 17
100 static_assert(test_all<int, short>());
101 static_assert(test_all<X, int>());
102 static_assert(test_all<Y, int>());
103 #endif
104 {
105 typedef Z T;
106 typedef int U;
107 optional<U> rhs;
108 test<T>(rhs);
109 }
110 {
111 typedef Z T;
112 typedef int U;
113 optional<U> rhs(U{3});
114 test<T>(rhs, true);
115 }
116
117 static_assert(!(std::is_constructible<optional<X>, const optional<Y>&>::value), "");
118
119 return 0;
120 }
121