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

source code of libcxx/test/std/utilities/optional/optional.comp_with_t/less_than.pass.cpp