Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::in_range

      From cppreference.com
      <cpp‎ |utility
       
       
      Utilities library
       
      Defined in header<utility>
      template<class R,class T>
      constexprbool in_range( T t)noexcept;
      (since C++20)

      Returnstrue if the value oft is in the range of values that can be represented inR, that is, ift can be converted toR in a value-preserving manner.

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

      Contents

      [edit]Parameters

      t - value to test

      [edit]Return value

      true if the value oft is representable inR,false otherwise.

      [edit]Possible implementation

      template<class R,class T>constexprbool in_range(T t)noexcept{returnstd::cmp_greater_equal(t,std::numeric_limits<R>::min())&&std::cmp_less_equal(t,std::numeric_limits<R>::max());}

      [edit]Notes

      This function cannot be used withenums (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

      Run this code
      #include <iostream>#include <utility> int main(){std::cout<<std::boolalpha; std::cout<< std::in_range<std::size_t>(-1)<<'\n';std::cout<< std::in_range<std::size_t>(42)<<'\n';}

      Output:

      falsetrue

      [edit]See also

      returns the smaller of the given values
      (algorithm function object)[edit]
      returns the greater of the given values
      (algorithm function object)[edit]
      clamps a value between a pair of boundary values
      (algorithm function object)[edit]
      (C++20)
      linear interpolation function
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/in_range&oldid=166104"

      [8]ページ先頭

      ©2009-2025 Movatter.jp