Namespaces
Variants

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

From cppreference.net
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
cmp_equal cmp_less cmp_less_than
(C++20) (C++20) (C++20)
cmp_not_equal cmp_greater cmp_greater_than
(C++20) (C++20) (C++20)
(C++20)
Swap and type operations
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)


Definiert in Header <utility>
template < class T, class U >
constexpr bool cmp_equal ( T t, U u ) noexcept ;
(1) (seit C++20)
template < class T, class U >
constexpr bool cmp_not_equal ( T t, U u ) noexcept ;
(2) (seit C++20)
template < class T, class U >
constexpr bool cmp_less ( T t, U u ) noexcept ;
(3) (seit C++20)
template < class T, class U >
constexpr bool cmp_greater ( T t, U u ) noexcept ;
(4) (seit C++20)
template < class T, class U >
constexpr bool cmp_less_equal ( T t, U u ) noexcept ;
(5) (seit C++20)
template < class T, class U >
constexpr bool cmp_greater_equal ( T t, U u ) noexcept ;
(6) (seit C++20)

Vergleichen Sie die Werte zweier Ganzzahlen t und u . Im Gegensatz zu eingebauten Vergleichsoperatoren vergleichen sich negative vorzeichenbehaftete Ganzzahlen immer kleiner als (und nicht gleich ) vorzeichenlose Ganzzahlen: Der Vergleich ist sicher gegen nicht-werterhaltende Ganzzahlkonvertierung.

-1 > 0u; // wahr
std::cmp_greater(-1, 0u); // falsch

Es ist ein Kompilierzeitfehler, wenn entweder T oder U ein Nicht- Integer -Typ, ein Zeichentyp oder bool ist.

Inhaltsverzeichnis

Parameter

t - linker Operand
u - rechter Operand

Rückgabewert

1) true wenn t gleich u ist.
2) true falls t ungleich u ist.
3) true wenn t kleiner als u ist.
4) true wenn t größer als u ist.
5) true wenn t kleiner oder gleich u ist.
6) true wenn t größer oder gleich u ist.

Mögliche Implementierung

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);
}

Hinweise

Diese Funktionen können nicht zum Vergleichen von enums (einschließlich std::byte ), char , char8_t , char16_t , char32_t , wchar_t und bool verwendet werden.

Feature-Test Makro Wert Std Funktion
__cpp_lib_integer_comparison_functions 202002L (C++20) Integer-Vergleichsfunktionen

Beispiel

Das folgende Beispiel könnte eine Warnung zum Vergleich unterschiedlicher Vorzeichen erzeugen, wenn es ohne entsprechende Warnungsunterdrückungsflag kompiliert wird, z.B. -Wno-sign-compare (gcc/clang mit -Wall -Wextra , siehe auch SO: Deaktivieren einer spezifischen Warnung ).

#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));
}

Siehe auch

Funktionsobjekt, das x == y implementiert
(Klassentemplate)
Funktionsobjekt, das x ! = y implementiert
(Klassentemplate)
Funktionsobjekt, das x < y implementiert
(Klassentemplate)
Funktionsobjekt, das x > y implementiert
(Klassentemplate)
Funktionsobjekt, das x <= y implementiert
(Klassentemplate)
Funktionsobjekt, das x >= y implementiert
(Klassentemplate)
eingeschränktes Funktionsobjekt, das x == y implementiert
(Klasse)
eingeschränktes Funktionsobjekt, das x ! = y implementiert
(Klasse)
eingeschränktes Funktionsobjekt, das x < y implementiert
(Klasse)
eingeschränktes Funktionsobjekt, das x > y implementiert
(Klasse)
eingeschränktes Funktionsobjekt, das x <= y implementiert
(Klasse)
eingeschränktes Funktionsobjekt, das x >= y implementiert
(Klasse)
eingeschränktes Funktionsobjekt, das x <=> y implementiert
(Klasse)
(C++20)
prüft, ob ein ganzzahliger Wert im Wertebereich eines gegebenen Ganzzahltyps liegt
(Funktionstemplate)
bietet eine Schnittstelle zur Abfrage von Eigenschaften aller fundamentalen numerischen Typen
(Klassentemplate)