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)


定义于头文件 <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) true t 等于 u
2) true t 不等于 u
3) true t 小于 u 时。
4) true t 大于 u
5) true t 小于或等于 u
6) true t 大于或等于 u

可能的实现

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 配合 -Wall -Wextra 的情况,另请参阅 SO: 禁用特定警告 ),可能会产生 有符号性比较差异 警告。

#include <utility>
// 取消下一行的注释将禁用“有符号/无符号比较”警告:
// #pragma GCC diagnostic ignored "-Wsign-compare"
int main()
{
    static_assert(sizeof(int) == 4); // 前置条件
    // 令人惊讶的是
    static_assert(-1 > 1U); //< 警告:有符号-无符号比较
    // 因为 -1 隐式转换为右侧类型(`unsigned int`)后
    // 表达式等价于:
    static_assert(0xFFFFFFFFU > 1U);
    static_assert(0xFFFFFFFFU == static_cast<unsigned>(-1));
    // 相比之下,cmp_* 系列函数按最符合直觉的方式比较整数 ——
    // 有符号负整数始终小于无符号整数:
    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); //< 警告:有符号-无符号比较
    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)
检查整数值是否在给定整数类型的范围内
(函数模板)
提供查询所有基础数值类型属性的接口
(类模板)