Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |utility
       
       
      Utilities library
      General utilities
      Relational operators(deprecated in C++20)
      Integer comparison functions
      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)
      Swap andtype operations
      (C++11)
      (C++11)
      (C++17)
      Common vocabulary types
      (C++11)
      (C++17)
      (C++17)
      (C++17)
      (C++11)
      (C++17)
      (C++23)


       
      Defined in header<utility>
      template<class T,class U>
      constexprbool cmp_equal( T t, U u)noexcept;
      (1)(since C++20)
      template<class T,class U>
      constexprbool cmp_not_equal( T t, U u)noexcept;
      (2)(since C++20)
      template<class T,class U>
      constexprbool cmp_less( T t, U u)noexcept;
      (3)(since C++20)
      template<class T,class U>
      constexprbool cmp_greater( T t, U u)noexcept;
      (4)(since C++20)
      template<class T,class U>
      constexprbool cmp_less_equal( T t, U u)noexcept;
      (5)(since C++20)
      template<class T,class U>
      constexprbool cmp_greater_equal( T t, U u)noexcept;
      (6)(since C++20)

      Compare the values of two integerst andu. Unlike builtin comparison operators, negative signed integers always compareless than (andnot equal to) unsigned integers: the comparison is safe against non-value-preserving integer conversion.

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

      It is a compile-time error if eitherT orU is a non-integer type, a character type, orbool.

      Contents

      [edit]Parameters

      t - left-hand argument
      u - right-hand argument

      [edit]Return value

      1)true ift is equal tou.
      2)true ift is not equal tou.
      3)true ift is less thanu.
      4)true ift is greater thanu.
      5)true ift is less or equal tou.
      6)true ift is greater or equal tou.

      [edit]Possible implementation

      template<class T,class U>constexprbool cmp_equal(T t, U u)noexcept{ifconstexpr(std::is_signed_v<T>==std::is_signed_v<U>)return t== u;elseifconstexpr(std::is_signed_v<T>)return t>=0&&std::make_unsigned_t<T>(t)== u;elsereturn u>=0&&std::make_unsigned_t<U>(u)== t;} template<class T,class U>constexprbool cmp_not_equal(T t, U u)noexcept{return!cmp_equal(t, u);} template<class T,class U>constexprbool cmp_less(T t, U u)noexcept{ifconstexpr(std::is_signed_v<T>==std::is_signed_v<U>)return t< u;elseifconstexpr(std::is_signed_v<T>)return t<0||std::make_unsigned_t<T>(t)< u;elsereturn u>=0&& t<std::make_unsigned_t<U>(u);} template<class T,class U>constexprbool cmp_greater(T t, U u)noexcept{return cmp_less(u, t);} template<class T,class U>constexprbool cmp_less_equal(T t, U u)noexcept{return!cmp_less(u, t);} template<class T,class U>constexprbool cmp_greater_equal(T t, U u)noexcept{return!cmp_less(t, u);}

      [edit]Notes

      These functions cannot be used to compareenums (includingstd::byte),char,char8_t,char16_t,char32_t,wchar_t andbool.

      Feature-test macroValueStdFeature
      __cpp_lib_integer_comparison_functions202002L(C++20)Integer comparison functions

      [edit]Example

      The example below might producedifferent signedness comparison warning if compiled without an appropriate warning suppression flag, e.g.,-Wno-sign-compare (gcc/clang with-Wall -Wextra, see alsoSO: disabling a specific warning).

      Run this code
      #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));}

      [edit]See also

      function object implementingx== y
      (class template)[edit]
      function object implementingx!= y
      (class template)[edit]
      function object implementingx< y
      (class template)[edit]
      function object implementingx> y
      (class template)[edit]
      function object implementingx<= y
      (class template)[edit]
      function object implementingx>= y
      (class template)[edit]
      constrained function object implementingx== y
      (class)[edit]
      constrained function object implementingx!= y
      (class)[edit]
      constrained function object implementingx< y
      (class)[edit]
      constrained function object implementingx> y
      (class)[edit]
      constrained function object implementingx<= y
      (class)[edit]
      constrained function object implementingx>= y
      (class)[edit]
      constrained function object implementingx<=> y
      (class)[edit]
      (C++20)
      checks if an integer value is in the range of a given integer type
      (function template)[edit]
      provides an interface to query properties of all fundamental numeric types
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/intcmp&oldid=166103"

      [8]ページ先頭

      ©2009-2025 Movatter.jp