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, c++17, c++20
10
11// template<class T2, class E2> requires (is_void_v<T2>)
12// friend constexpr bool operator==(const expected& x, const expected<T2, E2>& y);
13
14#include <cassert>
15#include <concepts>
16#include <expected>
17#include <type_traits>
18#include <utility>
19
20#include "test_macros.h"
21#include "../../types.h"
22
23struct Foo{};
24static_assert(!CanCompare<Foo, Foo>);
25
26static_assert(CanCompare<std::expected<void, int>, std::expected<void, int>>);
27static_assert(CanCompare<std::expected<void, int>, std::expected<void, short>>);
28
29#if TEST_STD_VER >= 26
30// https://wg21.link/P3379R0
31static_assert(!CanCompare<std::expected<void, int>, std::expected<int, int>>);
32static_assert(CanCompare<std::expected<void, int>, std::expected<void, int>>);
33static_assert(CanCompare<std::expected<void, int>, std::expected<void, int>>);
34static_assert(!CanCompare<std::expected<void, NonComparable>, std::expected<void, NonComparable>>);
35static_assert(!CanCompare<std::expected<void, int>, std::expected<void, NonComparable>>);
36static_assert(!CanCompare<std::expected<void, NonComparable>, std::expected<void, int>>);
37#else
38// Note this is true because other overloads in expected<non-void> are unconstrained
39static_assert(CanCompare<std::expected<void, int>, std::expected<int, int>>);
40#endif
41
42constexpr bool test() {
43 // x.has_value() && y.has_value()
44 {
45 const std::expected<void, int> e1;
46 const std::expected<void, int> e2;
47 assert(e1 == e2);
48 }
49
50 // !x.has_value() && y.has_value()
51 {
52 const std::expected<void, int> e1(std::unexpect, 5);
53 const std::expected<void, int> e2;
54 assert(e1 != e2);
55 }
56
57 // x.has_value() && !y.has_value()
58 {
59 const std::expected<void, int> e1;
60 const std::expected<void, int> e2(std::unexpect, 10);
61 const std::expected<void, int> e3(std::unexpect, 5);
62 assert(e1 != e2);
63 assert(e1 != e3);
64 }
65
66 // !x.has_value() && !y.has_value()
67 {
68 const std::expected<void, int> e1(std::unexpect, 5);
69 const std::expected<void, int> e2(std::unexpect, 10);
70 const std::expected<void, int> e3(std::unexpect, 5);
71 assert(e1 != e2);
72 assert(e1 == e3);
73 }
74
75 return true;
76}
77
78int main(int, char**) {
79 test();
80 static_assert(test());
81 return 0;
82}
83

source code of libcxx/test/std/utilities/expected/expected.void/equality/equality.other_expected.pass.cpp