std::cmp_equal, cmp_not_equal, cmp_less, cmp_greater, cmp_less_equal, cmp_greater_equal
From 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以降) |
2つの整数 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 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) |
整数値が指定された整数型の範囲内にあるかを確認する (関数テンプレート) |
| 全ての基本数値型のプロパティを問い合わせるインターフェースを提供する (クラステンプレート) |