blob: 8726f95a00c0ef5ec96e205b0d2e2097f213ee2d [file] [log] [blame]
Howard Hinnant01afa5c2013-09-02 20:30:37 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <optional>
11
12// optional(const optional<T>& rhs);
13
Marshall Clow0cdbe602013-11-15 22:42:10 +000014#include <experimental/optional>
Howard Hinnant01afa5c2013-09-02 20:30:37 +000015#include <type_traits>
16#include <cassert>
17
18#if _LIBCPP_STD_VER > 11
19
Marshall Clow0cdbe602013-11-15 22:42:10 +000020using std::experimental::optional;
21
Howard Hinnant01afa5c2013-09-02 20:30:37 +000022template <class T>
23void
Marshall Clow0cdbe602013-11-15 22:42:10 +000024test(const optional<T>& rhs, bool is_going_to_throw = false)
Howard Hinnant01afa5c2013-09-02 20:30:37 +000025{
26 bool rhs_engaged = static_cast<bool>(rhs);
27 try
28 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000029 optional<T> lhs = rhs;
Howard Hinnant01afa5c2013-09-02 20:30:37 +000030 assert(is_going_to_throw == false);
31 assert(static_cast<bool>(lhs) == rhs_engaged);
32 if (rhs_engaged)
33 assert(*lhs == *rhs);
34 }
35 catch (int i)
36 {
37 assert(i == 6);
38 }
39}
40
41class X
42{
43 int i_;
44public:
45 X(int i) : i_(i) {}
46 X(const X& x) : i_(x.i_) {}
47 ~X() {i_ = 0;}
48 friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
49};
50
51class Y
52{
53 int i_;
54public:
55 Y(int i) : i_(i) {}
56 Y(const Y& x) : i_(x.i_) {}
57
58 friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;}
59};
60
61int count = 0;
62
63class Z
64{
65 int i_;
66public:
67 Z(int i) : i_(i) {}
68 Z(const Z&)
69 {
70 if (++count == 2)
71 throw 6;
72 }
73
74 friend constexpr bool operator==(const Z& x, const Z& y) {return x.i_ == y.i_;}
75};
76
77
78#endif // _LIBCPP_STD_VER > 11
79
80int main()
81{
82#if _LIBCPP_STD_VER > 11
83 {
84 typedef int T;
Marshall Clow0cdbe602013-11-15 22:42:10 +000085 optional<T> rhs;
Howard Hinnant01afa5c2013-09-02 20:30:37 +000086 test(rhs);
87 }
88 {
89 typedef int T;
Marshall Clow0cdbe602013-11-15 22:42:10 +000090 optional<T> rhs(3);
Howard Hinnant01afa5c2013-09-02 20:30:37 +000091 test(rhs);
92 }
93 {
94 typedef X T;
Marshall Clow0cdbe602013-11-15 22:42:10 +000095 optional<T> rhs;
Howard Hinnant01afa5c2013-09-02 20:30:37 +000096 test(rhs);
97 }
98 {
99 typedef X T;
Marshall Clow0cdbe602013-11-15 22:42:10 +0000100 optional<T> rhs(X(3));
Howard Hinnant01afa5c2013-09-02 20:30:37 +0000101 test(rhs);
102 }
103 {
104 typedef Y T;
Marshall Clow0cdbe602013-11-15 22:42:10 +0000105 optional<T> rhs;
Howard Hinnant01afa5c2013-09-02 20:30:37 +0000106 test(rhs);
107 }
108 {
109 typedef Y T;
Marshall Clow0cdbe602013-11-15 22:42:10 +0000110 optional<T> rhs(Y(3));
Howard Hinnant01afa5c2013-09-02 20:30:37 +0000111 test(rhs);
112 }
113 {
114 typedef Z T;
Marshall Clow0cdbe602013-11-15 22:42:10 +0000115 optional<T> rhs;
Howard Hinnant01afa5c2013-09-02 20:30:37 +0000116 test(rhs);
117 }
118 {
119 typedef Z T;
Marshall Clow0cdbe602013-11-15 22:42:10 +0000120 optional<T> rhs(Z(3));
Howard Hinnant01afa5c2013-09-02 20:30:37 +0000121 test(rhs, true);
122 }
123#endif // _LIBCPP_STD_VER > 11
124}