std::cmp_equal, cmp_not_equal, cmp_less, cmp_greater, cmp_less_equal, cmp_greater_equal
definiert in Header <Hilfsfunktionen>
|
||
template< class T, class U > constexpr bool cmp_equal( T t, U u ) noexcept; |
(1) | |
template< class T, class U > constexpr bool cmp_not_equal( T t, U u ) noexcept; |
(2) | |
template< class T, class U > constexpr bool cmp_less( T t, U u ) noexcept; |
(3) | |
template< class T, class U > constexpr bool cmp_greater( T t, U u ) noexcept; |
(4) | |
template< class T, class U > constexpr bool cmp_less_equal( T t, U u ) noexcept; |
(5) | |
template< class T, class U > constexpr bool cmp_greater_equal( T t, U u ) noexcept; |
(6) | |
Vergleicht zwei ganzahlige Werte t
und u
. Anders als eingebaute Vergleichsoperatoren werden negative, vorzeichenbehaftete Ganzzahlen immer als kleiner als (und nicht gleich zu) in Bezug auf nicht-vorzeichenbehaftete Ganzzahlen angegeben. Der Vergleich ist sicher bezüglich verlustbehafteter Ganzzahlenkonvertierung.
-1 > 0u; // true std::cmp_greater(-1, 0u); // false
Es führt zu einem Kompilationsfehler, falls entweder T
oder U
kein nicht-vorzeichen- oder vorzeigenbehaftete Ganzzahltyp ist. Dieses schließt Ganzzahlen des Standards und erweiterte Ganzzahlen ein.
Inhaltsverzeichnis |
[Bearbeiten] Parameter
t | - | Argument auf der linken Seite |
u | - | Argument auf der rechten Seite |
[Bearbeiten] Rückgabewerte
t
gleich zu u
ist.t
ungleich zu u
ist.t
kleiner als u
ist.t
größer als u
ist.t
kleiner als oder gleich zu u
ist.t
größer als oder gleich zu u
ist.[Bearbeiten] Mögliche Implementierungen
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); } |
[Bearbeiten] Anmerkungen
Diese Funktionen können nicht benutzt werden, um enums (einschließlich std::byte), char, char8_t, char16_t, char32_t, wchar_t oder bool zu vergleichen.
Feature testing macro: __cpp_lib_integer_comparison_functions
[Bearbeiten] Beispiele
Das Beispiele unten könnte Warnungen über different signedness comparison erzeugen, falls es nicht mit entsprechender Warnungsunterdrückung kompiliert wird, wie z.B. -Wno-sign-compare
(gcc/clang mit -Wall -Wextra
, siehe auch 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 ) ); }
[Bearbeiten] Referenzen
Funktions-Objekt Umsetzung x == y Original: function object implementing x == y The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (Klassen-Template) | |
Funktions-Objekt Umsetzung x != y Original: function object implementing x != y The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (Klassen-Template) | |
Funktions-Objekt Umsetzung x < y Original: function object implementing x < y The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (Klassen-Template) | |
Funktions-Objekt Umsetzung x > y Original: function object implementing x > y The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (Klassen-Template) | |
Funktions-Objekt Umsetzung x <= y Original: function object implementing x <= y The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (Klassen-Template) | |
Funktions-Objekt Umsetzung x >= y Original: function object implementing x >= y The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (Klassen-Template) | |
(C++20) |
Funktionsojekt, daß x = y implementiert (Klasse) |
(C++20) |
Funktionsojekt, daß x != y implementiert (Klasse) |
(C++20) |
Funktionsojekt, daß x < y implementiert (Klasse) |
(C++20) |
Funktionsojekt, daß x > y implementiert (Klasse) |
(C++20) |
Funktionsojekt, daß x <= y implementiert (Klasse) |
(C++20) |
Funktionsojekt, daß x >= y implementiert (Klasse) |
(C++20) |
Funktionsobjekt, daß x <=> y implementiert (Klasse) |
(C++20) |
überprüft, ob ein Ganzzahlwert im Wertebereich eines gegebenen Ganzzahltyps liegt (Funktions-Template) |
bietet eine Schnittstelle zur Abfrage von Eigenschaften aller grundlegenden numerischen Typen . Original: provides an interface to query properties of all fundamental numeric types. The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (Klassen-Template) |