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
10
11// template<class T>
12// concept totally_ordered;
13
14#include <concepts>
15
16#include <array>
17#include <deque>
18#include <forward_list>
19#include <list>
20#include <map>
21#include <memory>
22#include <optional>
23#include <set>
24#include <unordered_map>
25#include <unordered_set>
26#include <vector>
27
28#include "compare_types.h"
29#include "test_macros.h"
30
31// `models_totally_ordered` checks that `std::totally_ordered` subsumes
32// `std::equality_comparable`. This overload should *never* be called.
33template <std::equality_comparable T>
34constexpr bool models_totally_ordered() noexcept {
35 return false;
36}
37
38template <std::totally_ordered T>
39constexpr bool models_totally_ordered() noexcept {
40 return true;
41}
42
43namespace fundamentals {
44static_assert(models_totally_ordered<int>());
45static_assert(models_totally_ordered<double>());
46static_assert(models_totally_ordered<void*>());
47static_assert(models_totally_ordered<char*>());
48static_assert(models_totally_ordered<char const*>());
49static_assert(models_totally_ordered<char volatile*>());
50static_assert(models_totally_ordered<char const volatile*>());
51static_assert(models_totally_ordered<wchar_t&>());
52static_assert(models_totally_ordered<char8_t const&>());
53static_assert(models_totally_ordered<char16_t volatile&>());
54static_assert(models_totally_ordered<char32_t const volatile&>());
55static_assert(models_totally_ordered<unsigned char&&>());
56static_assert(models_totally_ordered<unsigned short const&&>());
57static_assert(models_totally_ordered<unsigned int volatile&&>());
58static_assert(models_totally_ordered<unsigned long const volatile&&>());
59static_assert(models_totally_ordered<int[5]>());
60static_assert(models_totally_ordered<int (*)(int)>());
61static_assert(models_totally_ordered<int (&)(int)>());
62static_assert(models_totally_ordered<int (*)(int) noexcept>());
63static_assert(models_totally_ordered<int (&)(int) noexcept>());
64
65#ifndef TEST_COMPILER_GCC
66static_assert(!std::totally_ordered<std::nullptr_t>);
67#endif
68
69struct S {};
70static_assert(!std::totally_ordered<S>);
71static_assert(!std::totally_ordered<int S::*>);
72static_assert(!std::totally_ordered<int (S::*)()>);
73static_assert(!std::totally_ordered<int (S::*)() noexcept>);
74static_assert(!std::totally_ordered<int (S::*)() &>);
75static_assert(!std::totally_ordered<int (S::*)() & noexcept>);
76static_assert(!std::totally_ordered<int (S::*)() &&>);
77static_assert(!std::totally_ordered < int (S::*)() && noexcept >);
78static_assert(!std::totally_ordered<int (S::*)() const>);
79static_assert(!std::totally_ordered<int (S::*)() const noexcept>);
80static_assert(!std::totally_ordered<int (S::*)() const&>);
81static_assert(!std::totally_ordered<int (S::*)() const & noexcept>);
82static_assert(!std::totally_ordered<int (S::*)() const&&>);
83static_assert(!std::totally_ordered < int (S::*)() const&& noexcept >);
84static_assert(!std::totally_ordered<int (S::*)() volatile>);
85static_assert(!std::totally_ordered<int (S::*)() volatile noexcept>);
86static_assert(!std::totally_ordered<int (S::*)() volatile&>);
87static_assert(!std::totally_ordered<int (S::*)() volatile & noexcept>);
88static_assert(!std::totally_ordered<int (S::*)() volatile&&>);
89static_assert(!std::totally_ordered < int (S::*)() volatile&& noexcept >);
90static_assert(!std::totally_ordered<int (S::*)() const volatile>);
91static_assert(!std::totally_ordered<int (S::*)() const volatile noexcept>);
92static_assert(!std::totally_ordered<int (S::*)() const volatile&>);
93static_assert(!std::totally_ordered<int (S::*)() const volatile & noexcept>);
94static_assert(!std::totally_ordered<int (S::*)() const volatile&&>);
95static_assert(!std::totally_ordered < int (S::*)() const volatile&& noexcept >);
96
97static_assert(!std::totally_ordered<void>);
98} // namespace fundamentals
99
100namespace standard_types {
101static_assert(models_totally_ordered<std::array<int, 10> >());
102static_assert(models_totally_ordered<std::deque<int> >());
103static_assert(models_totally_ordered<std::forward_list<int> >());
104static_assert(models_totally_ordered<std::list<int> >());
105static_assert(models_totally_ordered<std::optional<int> >());
106static_assert(models_totally_ordered<std::set<int> >());
107static_assert(models_totally_ordered<std::vector<bool> >());
108static_assert(models_totally_ordered<std::vector<int> >());
109
110static_assert(!std::totally_ordered<std::unordered_map<int, void*> >);
111static_assert(!std::totally_ordered<std::unordered_set<int> >);
112
113struct A {};
114// FIXME(cjdb): uncomment when operator<=> is implemented for each of these types.
115// static_assert(!std::totally_ordered<std::array<A, 10> >);
116// static_assert(!std::totally_ordered<std::deque<A> >);
117// static_assert(!std::totally_ordered<std::forward_list<A> >);
118// static_assert(!std::totally_ordered<std::list<A> >);
119// static_assert(!std::totally_ordered<std::set<A> >);
120// static_assert(!std::totally_ordered<std::vector<A> >);
121} // namespace standard_types
122
123namespace types_fit_for_purpose {
124static_assert(models_totally_ordered<member_three_way_comparable>());
125static_assert(models_totally_ordered<friend_three_way_comparable>());
126static_assert(models_totally_ordered<explicit_operators>());
127static_assert(models_totally_ordered<different_return_types>());
128static_assert(!std::totally_ordered<cxx20_member_eq>);
129static_assert(!std::totally_ordered<cxx20_friend_eq>);
130static_assert(!std::totally_ordered<one_member_one_friend>);
131static_assert(!std::totally_ordered<equality_comparable_with_ec1>);
132
133static_assert(!std::totally_ordered<no_eq>);
134static_assert(!std::totally_ordered<no_neq>);
135static_assert(!std::totally_ordered<no_lt>);
136static_assert(!std::totally_ordered<no_gt>);
137static_assert(!std::totally_ordered<no_le>);
138static_assert(!std::totally_ordered<no_ge>);
139
140static_assert(!std::totally_ordered<wrong_return_type_eq>);
141static_assert(!std::totally_ordered<wrong_return_type_ne>);
142static_assert(!std::totally_ordered<wrong_return_type_lt>);
143static_assert(!std::totally_ordered<wrong_return_type_gt>);
144static_assert(!std::totally_ordered<wrong_return_type_le>);
145static_assert(!std::totally_ordered<wrong_return_type_ge>);
146static_assert(!std::totally_ordered<wrong_return_type>);
147
148static_assert(!std::totally_ordered<cxx20_member_eq_operator_with_deleted_ne>);
149static_assert(!std::totally_ordered<cxx20_friend_eq_operator_with_deleted_ne>);
150static_assert(
151 !std::totally_ordered<member_three_way_comparable_with_deleted_eq>);
152static_assert(
153 !std::totally_ordered<member_three_way_comparable_with_deleted_ne>);
154static_assert(
155 !std::totally_ordered<friend_three_way_comparable_with_deleted_eq>);
156static_assert(
157 !std::totally_ordered<friend_three_way_comparable_with_deleted_ne>);
158
159static_assert(!std::totally_ordered<eq_returns_explicit_bool>);
160static_assert(!std::totally_ordered<ne_returns_explicit_bool>);
161static_assert(!std::totally_ordered<lt_returns_explicit_bool>);
162static_assert(!std::totally_ordered<gt_returns_explicit_bool>);
163static_assert(!std::totally_ordered<le_returns_explicit_bool>);
164static_assert(!std::totally_ordered<ge_returns_explicit_bool>);
165static_assert(std::totally_ordered<returns_true_type>);
166static_assert(std::totally_ordered<returns_int_ptr>);
167
168static_assert(std::totally_ordered<partial_ordering_totally_ordered_with>);
169static_assert(std::totally_ordered<weak_ordering_totally_ordered_with>);
170static_assert(std::totally_ordered<strong_ordering_totally_ordered_with>);
171} // namespace types_fit_for_purpose
172
173int main(int, char**) { return 0; }
174

source code of libcxx/test/std/concepts/concepts.compare/concepts.totallyordered/totally_ordered.pass.cpp