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> constexpr T optional<T>::value_or(U&& v) const&; |
13 | |
14 | #include <optional> |
15 | #include <type_traits> |
16 | #include <cassert> |
17 | |
18 | #include "test_macros.h" |
19 | |
20 | using std::optional; |
21 | |
22 | struct Y |
23 | { |
24 | int i_; |
25 | |
26 | constexpr Y(int i) : i_(i) {} |
27 | }; |
28 | |
29 | struct X |
30 | { |
31 | int i_; |
32 | |
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_;} |
38 | }; |
39 | |
40 | int main(int, char**) |
41 | { |
42 | { |
43 | constexpr optional<X> opt(2); |
44 | constexpr Y y(3); |
45 | static_assert(opt.value_or(u: y) == 2, "" ); |
46 | } |
47 | { |
48 | constexpr optional<X> opt(2); |
49 | static_assert(opt.value_or(u: Y(3)) == 2, "" ); |
50 | } |
51 | { |
52 | constexpr optional<X> opt; |
53 | constexpr Y y(3); |
54 | static_assert(opt.value_or(u: y) == 3, "" ); |
55 | } |
56 | { |
57 | constexpr optional<X> opt; |
58 | static_assert(opt.value_or(u: Y(3)) == 4, "" ); |
59 | } |
60 | { |
61 | const optional<X> opt(2); |
62 | const Y y(3); |
63 | assert(opt.value_or(y) == 2); |
64 | } |
65 | { |
66 | const optional<X> opt(2); |
67 | assert(opt.value_or(Y(3)) == 2); |
68 | } |
69 | { |
70 | const optional<X> opt; |
71 | const Y y(3); |
72 | assert(opt.value_or(y) == 3); |
73 | } |
74 | { |
75 | const optional<X> opt; |
76 | assert(opt.value_or(Y(3)) == 4); |
77 | } |
78 | |
79 | return 0; |
80 | } |
81 | |