Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::numeric_limits<T>::max

      From cppreference.com
      <cpp‎ |types‎ |numeric limits
       
       
      Utilities library
       
       
      std::numeric_limits
      Static constants
      Static member functions
      numeric_limits::max
      Helper types
       
      Defined in header<limits>
      static T max()throw();
      (until C++11)
      staticconstexpr T max()noexcept;
      (since C++11)

      Returns the maximum finite value representable by the numeric typeT. Meaningful for all bounded types.

      [edit]Return value

      Tstd::numeric_limits<T>::max()
      /* non-specialized */T()
      booltrue
      charCHAR_MAX
      signedcharSCHAR_MAX
      unsignedcharUCHAR_MAX
      wchar_tWCHAR_MAX
      char8_t(since C++20)UCHAR_MAX
      char16_t(since C++11)UINT_LEAST16_MAX
      char32_t(since C++11)UINT_LEAST32_MAX
      shortSHRT_MAX
      unsignedshortUSHRT_MAX
      intINT_MAX
      unsignedintUINT_MAX
      longLONG_MAX
      unsignedlongULONG_MAX
      longlong(since C++11)LLONG_MAX
      unsignedlonglong(since C++11)ULLONG_MAX
      floatFLT_MAX
      doubleDBL_MAX
      longdoubleLDBL_MAX

      [edit]Example

      Demonstrates the use ofmax() with some fundamental types and some standard library typedefs (the output is system-specific):

      Run this code
      #include <boost/type_index.hpp>#include <cstddef>#include <iomanip>#include <iostream>#include <limits>#include <type_traits> template<typename T>void print_max_value_of(){constexpr T max{std::numeric_limits<T>::max()}; std::cout<<std::setw(16)<< boost::typeindex::type_id<T>()<<": ";ifconstexpr(std::is_floating_point_v<T>)std::cout<<std::defaultfloat<< max<<" = "<<std::hexfloat<< max<<'\n';else{constexprauto m{static_cast<unsignedlonglong>(max)};std::cout<<std::dec<< m<<" = "<<std::hex<< m<<'\n';}} int main(){std::cout<<std::showbase;     print_max_value_of<bool>();    print_max_value_of<short>();    print_max_value_of<int>();    print_max_value_of<std::streamsize>();    print_max_value_of<std::size_t>();    print_max_value_of<char>();    print_max_value_of<char16_t>();    print_max_value_of<wchar_t>();    print_max_value_of<float>();    print_max_value_of<double>();    print_max_value_of<longdouble>();}

      Possible output:

                  bool: 1 = 0x1           short: 32767 = 0x7fff             int: 2147483647 = 0x7fffffff            long: 9223372036854775807 = 0x7fffffffffffffff   unsigned long: 18446744073709551615 = 0xffffffffffffffff            char: 127 = 0x7f        char16_t: 65535 = 0xffff         wchar_t: 2147483647 = 0x7fffffff           float: 3.40282e+38 = 0x1.fffffep+127          double: 1.79769e+308 = 0x1.fffffffffffffp+1023     long double: 1.18973e+4932 = 0xf.fffffffffffffffp+16380

      [edit]See also

      [static](C++11)
      returns the lowest finite value of the given type, i.e. the most negative value for signed types,0 for unsigned types
      (public static member function)[edit]
      [static]
      returns the smallest finite value of the given non-floating-point type, or the smallest positive normal value of the given floating-point type
      (public static member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/numeric_limits/max&oldid=178154"

      [8]ページ先頭

      ©2009-2025 Movatter.jp