104 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#include <type_traits>16#include <cassert>17 18#include "test_comparisons.h"19#include "test_macros.h"20 21#if TEST_STD_VER >= 2622 23// Test SFINAE.24 25static_assert(HasOperatorEqual<std::optional<int>>);26static_assert(HasOperatorEqual<std::optional<EqualityComparable>>);27static_assert(HasOperatorEqual<std::optional<EqualityComparable>, std::optional<int>>);28 29static_assert(!HasOperatorEqual<std::optional<NonComparable>>);30static_assert(!HasOperatorEqual<std::optional<EqualityComparable>, std::optional<NonComparable>>);31 32#endif33 34using std::optional;35 36struct X {37 int i_;38 39 constexpr X(int i) : i_(i) {}40};41 42constexpr bool operator==(const X& lhs, const X& rhs) {43 return lhs.i_ == rhs.i_;44}45 46int main(int, char**) {47 {48 typedef X T;49 typedef optional<T> O;50 51 constexpr O o1; // disengaged52 constexpr O o2; // disengaged53 constexpr O o3{1}; // engaged54 constexpr O o4{2}; // engaged55 constexpr O o5{1}; // engaged56 57 static_assert(o1 == o1, "");58 static_assert(o1 == o2, "");59 static_assert(!(o1 == o3), "");60 static_assert(!(o1 == o4), "");61 static_assert(!(o1 == o5), "");62 63 static_assert(o2 == o1, "");64 static_assert(o2 == o2, "");65 static_assert(!(o2 == o3), "");66 static_assert(!(o2 == o4), "");67 static_assert(!(o2 == o5), "");68 69 static_assert(!(o3 == o1), "");70 static_assert(!(o3 == o2), "");71 static_assert(o3 == o3, "");72 static_assert(!(o3 == o4), "");73 static_assert(o3 == o5, "");74 75 static_assert(!(o4 == o1), "");76 static_assert(!(o4 == o2), "");77 static_assert(!(o4 == o3), "");78 static_assert(o4 == o4, "");79 static_assert(!(o4 == o5), "");80 81 static_assert(!(o5 == o1), "");82 static_assert(!(o5 == o2), "");83 static_assert(o5 == o3, "");84 static_assert(!(o5 == o4), "");85 static_assert(o5 == o5, "");86 }87 {88 using O1 = optional<int>;89 using O2 = optional<long>;90 constexpr O1 o1(42);91 static_assert(o1 == O2(42), "");92 static_assert(!(O2(101) == o1), "");93 }94 {95 using O1 = optional<int>;96 using O2 = optional<const int>;97 constexpr O1 o1(42);98 static_assert(o1 == O2(42), "");99 static_assert(!(O2(101) == o1), "");100 }101 102 return 0;103}104