命名空间
变体
操作

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

来自 cppreference.cn
< 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)
检查整数值是否在给定整数类型的范围内
(函数模板) [编辑]
提供查询所有基本数值类型属性的接口
(类模板) [编辑]