名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | 工具
 
 
工具庫
通用工具
關係運算符 (C++20 中棄用)
整數比較函式
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)
交換型別操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
常用詞彙型別
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
在標頭檔案 <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 起)

比較兩個整數 tu 的值。與內建比較運算子不同,負的有符號整數總是與無符號整數比較為小於(且不等於):此比較對非值保留的整數轉換是安全的。

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

如果 TU 是非整數型別、字元型別或 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)、charchar8_tchar16_tchar32_twchar_tbool

特性測試 標準 特性
__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 的函式物件
(類模板) [編輯]
實現 x == y 的受限函式物件
(類) [編輯]
實現 x != y 的受限函式物件
(類) [編輯]
實現 x < y 的受限函式物件
(類) [編輯]
實現 x > y 的受限函式物件
(類) [編輯]
實現 x <= y 的受限函式物件
(類) [編輯]
實現 x >= y 的受限函式物件
(類) [編輯]
實現 x <=> y 的受限函式物件
(類) [編輯]
(C++20)
檢查整數值是否在給定整數型別的範圍內
(函式模板) [編輯]
提供查詢所有基本數值型別屬性的介面
(類模板) [編輯]