100 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410// <optional>11 12// template <class T, class U> constexpr bool operator>= (const optional<T>& x, const optional<U>& y);13 14#include <optional>15 16#include "test_comparisons.h"17#include "test_macros.h"18 19#if TEST_STD_VER >= 2620 21// Test SFINAE.22static_assert(HasOperatorGreaterThanEqual<std::optional<ThreeWayComparable>>);23static_assert(HasOperatorGreaterThanEqual<std::optional<ThreeWayComparable>, std::optional<int>>);24 25static_assert(!HasOperatorGreaterThanEqual<std::optional<NonComparable>>);26static_assert(!HasOperatorGreaterThanEqual<std::optional<EqualityComparable>>);27static_assert(!HasOperatorGreaterThanEqual<std::optional<ThreeWayComparable>, std::optional<NonComparable>>);28 29#endif30 31using std::optional;32 33struct X {34 int i_;35 36 constexpr X(int i) : i_(i) {}37};38 39constexpr bool operator>=(const X& lhs, const X& rhs) {40 return lhs.i_ >= rhs.i_;41}42 43int main(int, char**) {44 {45 typedef optional<X> O;46 47 constexpr O o1; // disengaged48 constexpr O o2; // disengaged49 constexpr O o3{1}; // engaged50 constexpr O o4{2}; // engaged51 constexpr O o5{1}; // engaged52 53 static_assert((o1 >= o1), "");54 static_assert((o1 >= o2), "");55 static_assert(!(o1 >= o3), "");56 static_assert(!(o1 >= o4), "");57 static_assert(!(o1 >= o5), "");58 59 static_assert((o2 >= o1), "");60 static_assert((o2 >= o2), "");61 static_assert(!(o2 >= o3), "");62 static_assert(!(o2 >= o4), "");63 static_assert(!(o2 >= o5), "");64 65 static_assert((o3 >= o1), "");66 static_assert((o3 >= o2), "");67 static_assert((o3 >= o3), "");68 static_assert(!(o3 >= o4), "");69 static_assert((o3 >= o5), "");70 71 static_assert((o4 >= o1), "");72 static_assert((o4 >= o2), "");73 static_assert((o4 >= o3), "");74 static_assert((o4 >= o4), "");75 static_assert((o4 >= o5), "");76 77 static_assert((o5 >= o1), "");78 static_assert((o5 >= o2), "");79 static_assert((o5 >= o3), "");80 static_assert(!(o5 >= o4), "");81 static_assert((o5 >= o5), "");82 }83 {84 using O1 = optional<int>;85 using O2 = optional<long>;86 constexpr O1 o1(42);87 static_assert(o1 >= O2(42), "");88 static_assert(!(O2(11) >= o1), "");89 }90 {91 using O1 = optional<int>;92 using O2 = optional<const int>;93 constexpr O1 o1(42);94 static_assert(o1 >= O2(42), "");95 static_assert(!(O2(1) >= o1), "");96 }97 98 return 0;99}100