Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/intcmp"

From cppreference.com
< cpp‎ | utility
(fix || operator breaking markup)
m
 
(3 intermediate revisions by 3 users not shown)
Line 30: Line 30:
 
{{dcl end}}
 
{{dcl end}}
  
Compare the values of two integers {{c|t}} and {{c|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.
+
Compare the values of two integers {{c|t}} and {{c|u}}. Unlike builtin comparison operators, negative signed integers always compare ''less than'' (and ''not equal to'') unsigned integers: the comparison is safe against non-value-preserving integer conversion.
  
 
{{source|1=
 
{{source|1=
Line 37: Line 37:
 
}}
 
}}
 
   
 
   
It is a compile-time error if either {{tt|T}} or {{tt|U}} is not a signed or unsigned integer type (including standard integer type and extended integer type).
+
It is a compile-time error if either {{tt|T}} or {{tt|U}} is a non-[[cpp/language/types#Integer types|integer]] type, a character type, or {{c/core|bool}}.
  
 
===Parameters===
 
===Parameters===
Line 93: Line 93:
 
constexpr bool cmp_less_equal(T t, U u) noexcept
 
constexpr bool cmp_less_equal(T t, U u) noexcept
 
{
 
{
     return !cmp_greater(t, u);
+
     return !cmp_less(u, t);
 
}
 
}
  
Line 106: Line 106:
 
These functions cannot be used to compare {{lt|cpp/language/enum}}s (including {{ltt std|cpp/types/byte}}), {{c/core|char}}, {{c/core|char8_t}}, {{c/core|char16_t}}, {{c/core|char32_t}}, {{c/core|wchar_t}} and {{c/core|bool}}.
 
These functions cannot be used to compare {{lt|cpp/language/enum}}s (including {{ltt std|cpp/types/byte}}), {{c/core|char}}, {{c/core|char8_t}}, {{c/core|char16_t}}, {{c/core|char32_t}}, {{c/core|wchar_t}} and {{c/core|bool}}.
  
{{feature test macro|__cpp_lib_integer_comparison_functions|value=202002L|std=C++20|{{ls|cpp/utility|Integer comparison functions}}}}
+
{{feature test macro|__cpp_lib_integer_comparison_functions|value=202002L|std=C++20|{{ls|cpp/utility#Integer comparison functions}}}}
  
 
===Example===
 
===Example===

Latest revision as of 01:34, 18 December 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
cmp_equalcmp_lesscmp_less_than
(C++20)(C++20)(C++20)  
cmp_not_equalcmp_greatercmp_greater_than
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(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 non-value-preserving integer conversion.

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

It is a compile-time error if either T or U is a non-integer type, a character type, or bool.

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
{
    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 && std::make_unsigned_t<T>(t) == u;
    else
        return u >= 0 && std::make_unsigned_t<U>(u) == t;
}
 
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
{
    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 || std::make_unsigned_t<T>(t) < u;
    else
        return u >= 0 && t < std::make_unsigned_t<U>(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_less(u, t);
}
 
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 enums (including std::byte), char, char8_t, char16_t, char32_t, wchar_t and bool.

Feature-test macro Value Std Feature
__cpp_lib_integer_comparison_functions 202002L (C++20) Integer comparison functions

[edit] Example

The example below might produce different signedness comparison warning if compiled without an appropriate warning suppression flag, e.g., -Wno-sign-compare (gcc/clang with -Wall -Wextra, see also SO: disabling a specific warning).

#include <utility>
 
// Uncommenting the next line will disable "signed/unsigned comparison" warnings:
// #pragma GCC diagnostic ignored "-Wsign-compare"
 
int main()
{
    static_assert(sizeof(int) == 4); // precondition
 
    // Quite surprisingly
    static_assert(-1 > 1U); //< warning: sign-unsign comparison
    // because after implicit conversion of -1 to the RHS type (`unsigned int`)
    // the expression is equivalent to:
    static_assert(0xFFFFFFFFU > 1U);
    static_assert(0xFFFFFFFFU == static_cast<unsigned>(-1));
 
    // In contrast, the cmp_* family compares integers as most expected -
    // negative signed integers always compare less than unsigned integers:
    static_assert(std::cmp_less(-1, 1U));
    static_assert(std::cmp_less_equal(-1, 1U));
    static_assert(!std::cmp_greater(-1, 1U));
    static_assert(!std::cmp_greater_equal(-1, 1U));
 
    static_assert(-1 == 0xFFFFFFFFU); //< warning: sign-unsign comparison
    static_assert(std::cmp_not_equal(-1, 0xFFFFFFFFU));
}

[edit] See also

function object implementing x == y
(class template) [edit]
function object implementing x != y
(class template) [edit]
function object implementing x < y
(class template) [edit]
function object implementing x > y
(class template) [edit]
function object implementing x <= y
(class template) [edit]
function object implementing x >= y
(class template) [edit]
constrained function object implementing x == y
(class) [edit]
constrained function object implementing x != y
(class) [edit]
constrained function object implementing x < y
(class) [edit]
constrained function object implementing x > y
(class) [edit]
constrained function object implementing x <= y
(class) [edit]
constrained function object implementing x >= y
(class) [edit]
constrained function object implementing x <=> y
(class) [edit]
(C++20)
checks if an integer value is in the range of a given integer type
(function template) [edit]
provides an interface to query properties of all fundamental numeric types
(class template) [edit]