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++98, c++03, c++11, c++14
10 // <optional>
11 
12 // template <class T, class U> constexpr bool operator==(const optional<T>& x, const optional<U>& y);
13 
14 #include <optional>
15 #include <type_traits>
16 #include <cassert>
17 
18 using std::optional;
19 
20 struct X {
21   int i_;
22 
23   constexpr X(int i) : i_(i) {}
24 };
25 
26 constexpr bool operator==(const X& lhs, const X& rhs) {
27   return lhs.i_ == rhs.i_;
28 }
29 
30 int main(int, char**) {
31   {
32     typedef X T;
33     typedef optional<T> O;
34 
35     constexpr O o1;    // disengaged
36     constexpr O o2;    // disengaged
37     constexpr O o3{1}; // engaged
38     constexpr O o4{2}; // engaged
39     constexpr O o5{1}; // engaged
40 
41     static_assert(o1 == o1, "");
42     static_assert(o1 == o2, "");
43     static_assert(!(o1 == o3), "");
44     static_assert(!(o1 == o4), "");
45     static_assert(!(o1 == o5), "");
46 
47     static_assert(o2 == o1, "");
48     static_assert(o2 == o2, "");
49     static_assert(!(o2 == o3), "");
50     static_assert(!(o2 == o4), "");
51     static_assert(!(o2 == o5), "");
52 
53     static_assert(!(o3 == o1), "");
54     static_assert(!(o3 == o2), "");
55     static_assert(o3 == o3, "");
56     static_assert(!(o3 == o4), "");
57     static_assert(o3 == o5, "");
58 
59     static_assert(!(o4 == o1), "");
60     static_assert(!(o4 == o2), "");
61     static_assert(!(o4 == o3), "");
62     static_assert(o4 == o4, "");
63     static_assert(!(o4 == o5), "");
64 
65     static_assert(!(o5 == o1), "");
66     static_assert(!(o5 == o2), "");
67     static_assert(o5 == o3, "");
68     static_assert(!(o5 == o4), "");
69     static_assert(o5 == o5, "");
70   }
71   {
72     using O1 = optional<int>;
73     using O2 = optional<long>;
74     constexpr O1 o1(42);
75     static_assert(o1 == O2(42), "");
76     static_assert(!(O2(101) == o1), "");
77   }
78   {
79     using O1 = optional<int>;
80     using O2 = optional<const int>;
81     constexpr O1 o1(42);
82     static_assert(o1 == O2(42), "");
83     static_assert(!(O2(101) == o1), "");
84   }
85 
86   return 0;
87 }
88