Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / optional / optional.object / optional.object.observe / value_or_const.pass.cpp
blobcf782f11378764d4e1c7de5d41cdd4af9256334d
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
10 // <optional>
12 // template <class U> constexpr T optional<T>::value_or(U&& v) const&;
14 #include <optional>
15 #include <type_traits>
16 #include <cassert>
18 #include "test_macros.h"
20 using std::optional;
22 struct Y
24 int i_;
26 constexpr Y(int i) : i_(i) {}
29 struct X
31 int i_;
33 constexpr X(int i) : i_(i) {}
34 constexpr X(const Y& y) : i_(y.i_) {}
35 constexpr X(Y&& y) : i_(y.i_+1) {}
36 friend constexpr bool operator==(const X& x, const X& y)
37 {return x.i_ == y.i_;}
40 int main(int, char**)
43 constexpr optional<X> opt(2);
44 constexpr Y y(3);
45 static_assert(opt.value_or(y) == 2, "");
48 constexpr optional<X> opt(2);
49 static_assert(opt.value_or(Y(3)) == 2, "");
52 constexpr optional<X> opt;
53 constexpr Y y(3);
54 static_assert(opt.value_or(y) == 3, "");
57 constexpr optional<X> opt;
58 static_assert(opt.value_or(Y(3)) == 4, "");
61 const optional<X> opt(2);
62 const Y y(3);
63 assert(opt.value_or(y) == 2);
66 const optional<X> opt(2);
67 assert(opt.value_or(Y(3)) == 2);
70 const optional<X> opt;
71 const Y y(3);
72 assert(opt.value_or(y) == 3);
75 const optional<X> opt;
76 assert(opt.value_or(Y(3)) == 4);
79 return 0;