std::numeric_limits<T>:: max
From cppreference.net
<
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 |
示例
演示了
max()
与一些基本类型及标准库类型定义的使用(输出结果与系统相关):
运行此代码
#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>() << ": "; if constexpr (std::is_floating_point_v<T>) std::cout << std::defaultfloat << max << " = " << std::hexfloat << max << '\n'; else { constexpr auto m{static_cast<unsigned long long>(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<long double>(); }
可能的输出:
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
参见
|
[static]
(C++11)
|
返回给定类型的最低有限值,即对于有符号类型为最负值,对于无符号类型为
0
(公开静态成员函数) |
|
[static]
|
返回给定非浮点类型的最小有限值,或给定浮点类型的最小正规格化值
(公开静态成员函数) |