Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / optional / optional.object / optional.object.ctor / in_place_t.pass.cpp
blob65276c5a01976f14143766c6c7ab623a34536ae9
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
11 // <optional>
13 // template <class... Args>
14 // constexpr explicit optional(in_place_t, Args&&... args);
16 #include <optional>
17 #include <type_traits>
18 #include <cassert>
20 #include "test_macros.h"
22 using std::optional;
23 using std::in_place_t;
24 using std::in_place;
26 class X
28 int i_;
29 int j_ = 0;
30 public:
31 X() : i_(0) {}
32 X(int i) : i_(i) {}
33 X(int i, int j) : i_(i), j_(j) {}
35 ~X() {}
37 friend bool operator==(const X& x, const X& y)
38 {return x.i_ == y.i_ && x.j_ == y.j_;}
41 class Y
43 int i_;
44 int j_ = 0;
45 public:
46 constexpr Y() : i_(0) {}
47 constexpr Y(int i) : i_(i) {}
48 constexpr Y(int i, int j) : i_(i), j_(j) {}
50 friend constexpr bool operator==(const Y& x, const Y& y)
51 {return x.i_ == y.i_ && x.j_ == y.j_;}
54 class Z
56 public:
57 Z(int) {TEST_THROW(6);}
61 int main(int, char**)
64 constexpr optional<int> opt(in_place, 5);
65 static_assert(static_cast<bool>(opt) == true, "");
66 static_assert(*opt == 5, "");
68 struct test_constexpr_ctor
69 : public optional<int>
71 constexpr test_constexpr_ctor(in_place_t, int i)
72 : optional<int>(in_place, i) {}
77 optional<const int> opt(in_place, 5);
78 assert(*opt == 5);
81 const optional<X> opt(in_place);
82 assert(static_cast<bool>(opt) == true);
83 assert(*opt == X());
86 const optional<X> opt(in_place, 5);
87 assert(static_cast<bool>(opt) == true);
88 assert(*opt == X(5));
91 const optional<X> opt(in_place, 5, 4);
92 assert(static_cast<bool>(opt) == true);
93 assert(*opt == X(5, 4));
96 constexpr optional<Y> opt(in_place);
97 static_assert(static_cast<bool>(opt) == true, "");
98 static_assert(*opt == Y(), "");
100 struct test_constexpr_ctor
101 : public optional<Y>
103 constexpr test_constexpr_ctor(in_place_t)
104 : optional<Y>(in_place) {}
109 constexpr optional<Y> opt(in_place, 5);
110 static_assert(static_cast<bool>(opt) == true, "");
111 static_assert(*opt == Y(5), "");
113 struct test_constexpr_ctor
114 : public optional<Y>
116 constexpr test_constexpr_ctor(in_place_t, int i)
117 : optional<Y>(in_place, i) {}
122 constexpr optional<Y> opt(in_place, 5, 4);
123 static_assert(static_cast<bool>(opt) == true, "");
124 static_assert(*opt == Y(5, 4), "");
126 struct test_constexpr_ctor
127 : public optional<Y>
129 constexpr test_constexpr_ctor(in_place_t, int i, int j)
130 : optional<Y>(in_place, i, j) {}
134 #ifndef TEST_HAS_NO_EXCEPTIONS
138 const optional<Z> opt(in_place, 1);
139 assert(false);
141 catch (int i)
143 assert(i == 6);
146 #endif
148 return 0;