Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/concepts/totally ordered"

From cppreference.com
< cpp‎ | concepts
m (Added Spanish link)
(Use partially-ordered-with.)
Line 6: Line 6:
 
template <class T>
 
template <class T>
 
concept totally_ordered =
 
concept totally_ordered =
   std::equality_comparable<T> &&
+
   std::equality_comparable<T> && __PartiallyOrderedWith<T, T>;
  requires(const std::remove_reference_t<T>& a,
+
          const std::remove_reference_t<T>& b) {
+
    { a <  b } -> boolean-testable;
+
    { a >  b } -> boolean-testable;
+
    { a <= b } -> boolean-testable;
+
    { a >= b } -> boolean-testable;
+
  };
+
 
}}
 
}}
 
{{dcl|since=c++20|num=2|1=
 
{{dcl|since=c++20|num=2|1=
Line 25: Line 18:
 
       const std::remove_reference_t<T>&,
 
       const std::remove_reference_t<T>&,
 
       const std::remove_reference_t<U>&>> &&
 
       const std::remove_reference_t<U>&>> &&
   requires(const std::remove_reference_t<T>& t,
+
   __PartiallyOrderedWith<T, U>;
          const std::remove_reference_t<U>& u) {
+
    { t <  u } -> boolean-testable;
+
    { t >  u } -> boolean-testable;
+
    { t <= u } -> boolean-testable;
+
    { t >= u } -> boolean-testable;
+
    { u <  t } -> boolean-testable;
+
    { u >  t } -> boolean-testable;
+
    { u <= t } -> boolean-testable;
+
    { u >= t } -> boolean-testable;
+
  };
+
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 42: Line 25:
  
 
@2@ The concept {{tt|totally_ordered_with<T, U>}} specifies that the comparison operators {{tt|1===,!=,<,>,<=,>=}} on (possibly mixed) {{tt|T}} and {{tt|U}} operands yield results consistent with a strict total order. Comparing mixed operands yields results equivalent to comparing the operands converted to their common type.
 
@2@ The concept {{tt|totally_ordered_with<T, U>}} specifies that the comparison operators {{tt|1===,!=,<,>,<=,>=}} on (possibly mixed) {{tt|T}} and {{tt|U}} operands yield results consistent with a strict total order. Comparing mixed operands yields results equivalent to comparing the operands converted to their common type.
 +
 +
In both definitions, {{tt|''__PartiallyOrderedWith''}} is the exposition-only concept described in {{ltt|cpp/utility/compare/three_way_comparable}}.
  
 
=== Semantic requirements ===
 
=== Semantic requirements ===

Revision as of 20:15, 14 December 2020

Defined in header <concepts>
template <class T>

concept totally_ordered =

  std::equality_comparable<T> && __PartiallyOrderedWith<T, T>;
(1) (since C++20)
template <class T, class U>

concept totally_ordered_with =
  std::totally_ordered<T> &&
  std::totally_ordered<U> &&
  std::equality_comparable_with<T, U> &&
  std::totally_ordered<
    std::common_reference_t<
      const std::remove_reference_t<T>&,
      const std::remove_reference_t<U>&>> &&

  __PartiallyOrderedWith<T, U>;
(2) (since C++20)
1) The concept totally_ordered<T> specifies that the comparison operators ==,!=,<,>,<=,>= on T yield results consistent with a strict total order on T.
2) The concept totally_ordered_with<T, U> specifies that the comparison operators ==,!=,<,>,<=,>= on (possibly mixed) T and U operands yield results consistent with a strict total order. Comparing mixed operands yields results equivalent to comparing the operands converted to their common type.

In both definitions, __PartiallyOrderedWith is the exposition-only concept described in three_way_comparable.

Semantic requirements

1) totally_ordered<T> is modeled only if, given lvalues a, b and c of type const std::remove_reference_t<T>:
  • Exactly one of bool(a < b), bool(a > b) and bool(a == b) is true;
  • If bool(a < b) and bool(b < c) are both true, then bool(a < c) is true;
  • bool(a > b) == bool(b < a)
  • bool(a >= b) == !bool(a < b)
  • bool(a <= b) == !bool(b < a)
2) totally_ordered_with<T, U> is satisfied only if, given any lvalue t of type const std::remove_reference_t<T> and any lvalue u of type const std::remove_reference_t<U>, and let C be std::common_reference_t<const std::remove_reference_t<T>&, const std::remove_reference_t<U>&> :
  • bool(t < u) == bool(C(t) < C(u))
  • bool(t > u) == bool(C(t) > C(u))
  • bool(t <= u) == bool(C(t) <= C(u))
  • bool(t >= u) == bool(C(t) >= C(u))
  • bool(u < t) == bool(C(u) < C(t))
  • bool(u > t) == bool(C(u) > C(t))
  • bool(u <= t) == bool(C(u) <= C(t))
  • bool(u >= t) == bool(C(u) >= C(t))

Equality preservation

Expressions declared in requires expressions of the standard library concepts are required to be equality-preserving (except where stated otherwise).

Implicit expression variations

A requires expression that uses an expression that is non-modifying for some constant lvalue operand also requires implicit expression variations.