std::cmp_equal, cmp_not_equal, cmp_less, cmp_greater, cmp_less_equal, cmp_greater_equal
来自 cppreference.cn
定义于头文件 <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。
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_integer_comparison_functions |
202002L |
(C++20) | 整数比较函数 |
[编辑] 示例
若编译时未使用适当的警告抑制标记,例如 -Wno-sign-compare
(gcc/clang 与 -Wall -Wextra
),下例可能产生有符号性比较不同警告(亦见 SO:禁用特定警告)。
运行此代码
#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) |
检查整数值是否在给定整数类型的范围内 (函数模板) |
提供查询所有基本数值类型属性的接口 (类模板) |