std::numeric_limits<T>::max
来自cppreference.com
                    
                                        
                    < cpp | types | numeric limits
                    
                                                            
                    |   在标头  <limits> 定义
  | 
||
|   static T max() throw();  | 
(C++11 前) | |
|   static constexpr T max() noexcept;  | 
(C++11 起) | |
返回数值类型 T 所能表示的最大有限值。对所有有界类型都有意义。
返回值
  T
 | 
std::numeric_limits<T>::max() | 
| /* 未特化 */ | T() | 
| bool | true | 
| char | CHAR_MAX | 
| signed char | SCHAR_MAX | 
| unsigned char | UCHAR_MAX | 
| wchar_t | WCHAR_MAX | 
| char8_t (C++20 起) | UCHAR_MAX | 
| char16_t (C++11 起) | UINT_LEAST16_MAX | 
| char32_t (C++11 起) | UINT_LEAST32_MAX | 
| short | SHRT_MAX | 
| unsigned short | USHRT_MAX | 
| int | INT_MAX | 
| unsigned int | UINT_MAX | 
| long | LONG_MAX | 
| unsigned long | ULONG_MAX | 
| long long (C++11 起) | LLONG_MAX | 
| unsigned long long (C++11 起) | ULLONG_MAX | 
| float | FLT_MAX | 
| double | DBL_MAX | 
| long double | LDBL_MAX | 
示例
以一些基本类型和一些标准库 typedef 演示 max() 的使用(输出是系统限定的):
运行此代码
#include <cstddef> #include <iostream> #include <limits> #include <string_view> template<typename T> void print_int_twice(std::string_view type, T value) { std::cout << type << ":" << std::dec << value << " 或 " << std::hex << value << '\n'; } template<typename T> void print_float_twice(std::string_view type, T value) { std::cout << type << ":" << std::defaultfloat << value << " 或 " << std::hexfloat << value << '\n'; } int main() { std::cout << std::showbase; print_int_twice("short", std::numeric_limits<short>::max()); print_int_twice("int", std::numeric_limits<int>::max()); print_int_twice("streamsize", std::numeric_limits<std::streamsize>::max()); print_int_twice("size_t", std::numeric_limits<std::size_t>::max()); print_float_twice("float", std::numeric_limits<float>::max()); print_float_twice("double", std::numeric_limits<double>::max()); print_float_twice("long double", std::numeric_limits<long double>::max()); }
可能的输出:
short:32767 或 0x7fff int:2147483647 或 0x7fffffff streamsize:9223372036854775807 或 0x7fffffffffffffff size_t:18446744073709551615 或 0xffffffffffffffff float:3.40282e+38 或 0x1.fffffep+127 double:1.79769e+308 或 0x1.fffffffffffffp+1023 long double:1.18973e+4932 或 0xf.fffffffffffffffp+16380
参阅
|    [静态] (C++11)  | 
   返回给定类型的最低有限值   (公开静态成员函数)  | 
|    [静态]  | 
   返回给定类型的最小有限值   (公开静态成员函数)  |