Namespaces
Variants
Views
Actions

std::cmp_equal, cmp_not_equal, cmp_less, cmp_greater, cmp_less_equal, cmp_greater_equal

From cppreference.com
< cpp‎ | utility
 
 
Utilities library
Type support (basic types, RTTI, type traits)
Dynamic memory management
Error handling
Program utilities
Variadic functions
Library feature-test macros
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Comparisons (C++20)
Integer comparison functions
cmp_equalcmp_not_equalcmp_lesscmp_greatercmp_less_thancmp_greater_than
(C++20)(C++20)(C++20)(C++20)(C++20)(C++20)
(C++20)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

Swap, forward and move
(C++14)
(C++11)
(C++11)
Elementary string conversions
(C++17)
(C++17)
Type operations
(C++11)
(C++17)
(C++17)
 
Defined in header <utility>
template< class T, class U >
constexpr bool cmp_equal( T t, U u ) noexcept;
(1) (since C++20)
template< class T, class U >
constexpr bool cmp_not_equal( T t, U u ) noexcept;
(2) (since C++20)
template< class T, class U >
constexpr bool cmp_less( T t, U u ) noexcept;
(3) (since C++20)
template< class T, class U >
constexpr bool cmp_greater( T t, U u ) noexcept;
(4) (since C++20)
template< class T, class U >
constexpr bool cmp_less_equal( T t, U u ) noexcept;
(5) (since C++20)
template< class T, class U >
constexpr bool cmp_greater_equal( T t, U u ) noexcept;
(6) (since C++20)

Compare the values of two integers t and u. Unlike builtin comparison operators, negative signed integers always compare less than (and not equal to) unsigned integers: the comparison is safe against lossy integer conversion.

-1 > 0u; // true
std::cmp_greater(-1, 0u); // false

It is a compile-time error if either T or U is not a signed or unsigned integer type (including standard integer type and extended integer type).

Contents

[edit] Parameters

t - left-hand argument
u - right-hand argument

[edit] Return value

1) true if t is equal to u.
2) true if t is not equal to u.
3) true if t is less than u.
4) true if t is greater than u.
5) true if t is less or equal to u.
6) true if t is greater or equal to u.

[edit] Possible implementation

template< class T, class U >
constexpr bool cmp_equal( T t, U u ) noexcept
{
    using UT = std::make_unsigned_t<T>;
    using UU = std::make_unsigned_t<U>;
    if constexpr (std::is_signed_v<T> == std::is_signed_v<U>)
        return t == u;
    else if constexpr (std::is_signed_v<T>)
        return t < 0 ? false : UT(t) == u;
    else
        return u < 0 ? false : t == UU(u);
}
 
template< class T, class U >
constexpr bool cmp_not_equal( T t, U u ) noexcept
{
    return !cmp_equal(t, u);
}
 
template< class T, class U >
constexpr bool cmp_less( T t, U u ) noexcept
{
    using UT = std::make_unsigned_t<T>;
    using UU = std::make_unsigned_t<U>;
    if constexpr (std::is_signed_v<T> == std::is_signed_v<U>)
        return t < u;
    else if constexpr (std::is_signed_v<T>)
        return t < 0 ? true : UT(t) < u;
    else
        return u < 0 ? false : t < UU(u);
}
 
template< class T, class U >
constexpr bool cmp_greater( T t, U u ) noexcept
{
    return cmp_less(u, t);
}
 
template< class T, class U >
constexpr bool cmp_less_equal( T t, U u ) noexcept
{
    return !cmp_greater(t, u);
}
 
template< class T, class U >
constexpr bool cmp_greater_equal( T t, U u ) noexcept
{
    return !cmp_less(t, u);
}

[edit] Notes

These functions cannot be used to compare std::byte, char, char8_t, char16_t, char32_t, wchar_t and bool.

[edit] Example