std::cmp_equal, cmp_not_equal, cmp_less, cmp_greater, cmp_less_equal, cmp_greater_equal
來自 cppreference.com
在標頭檔案 <utility> 中定義 |
||
template< class T, class U > constexpr bool cmp_equal( T t, U u ) noexcept; |
(1) | (C++20 起) |
template< class T, class U > constexpr bool cmp_not_equal( T t, U u ) noexcept; |
(2) | (C++20 起) |
template< class T, class U > constexpr bool cmp_less( T t, U u ) noexcept; |
(3) | (C++20 起) |
template< class T, class U > constexpr bool cmp_greater( T t, U u ) noexcept; |
(4) | (C++20 起) |
template< class T, class U > constexpr bool cmp_less_equal( T t, U u ) noexcept; |
(5) | (C++20 起) |
template< class T, class U > constexpr bool cmp_greater_equal( T t, U u ) noexcept; |
(6) | (C++20 起) |
比較兩個整數 t 和 u 的值。與內建比較運算子不同,負的有符號整數總是與無符號整數比較為小於(且不等於):此比較對非值保留的整數轉換是安全的。
-1 > 0u; // true std::cmp_greater(-1, 0u); // false
如果 T
或 U
是非整數型別、字元型別或 bool,則會產生編譯時錯誤。
目錄 |
[編輯] 引數
t | - | 左側引數 |
u | - | 右側引數 |
[編輯] 返回值
1) 如果 t 等於 u,則為 true。
2) 如果 t 不等於 u,則為 true。
3) 如果 t 小於 u,則為 true。
4) 如果 t 大於 u,則為 true。
5) 如果 t 小於或等於 u,則為 true。
6) 如果 t 大於或等於 u,則為 true。
[編輯] 可能實現
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); } |
[編輯] 注意
這些函式不能用於比較列舉(包括std::byte)、char、char8_t、char16_t、char32_t、wchar_t 和 bool。
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_integer_comparison_functions |
202002L |
(C++20) | 整數比較函式 |
[編輯] 示例
如果編譯時未指定適當的警告抑制標誌(例如 -Wno-sign-compare
(gcc/clang with -Wall -Wextra
,另請參見 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)); }
[編輯] 另請參見
實現 x == y 的函式物件 (類模板) | |
實現 x != y 的函式物件 (類模板) | |
實現 x < y 的函式物件 (類模板) | |
實現 x > y 的函式物件 (類模板) | |
實現 x <= y 的函式物件 (類模板) | |
實現 x >= y 的函式物件 (類模板) | |
(C++20) |
實現 x == y 的受限函式物件 (類) |
(C++20) |
實現 x != y 的受限函式物件 (類) |
(C++20) |
實現 x < y 的受限函式物件 (類) |
(C++20) |
實現 x > y 的受限函式物件 (類) |
(C++20) |
實現 x <= y 的受限函式物件 (類) |
(C++20) |
實現 x >= y 的受限函式物件 (類) |
(C++20) |
實現 x <=> y 的受限函式物件 (類) |
(C++20) |
檢查整數值是否在給定整數型別的範圍內 (函式模板) |
提供查詢所有基本數值型別屬性的介面 (類模板) |