数值极限
定义于头文件 <limits>
定义于头文件 | ||
template< class T > class numeric_limits; |
numeric_limits 类模板提供查询各种算术类型属性的标准化方式(例如 int 类型的最大可能值是 std::numeric_limits<int>::max() )。
鉴别 std::numeric_limits 是否为该类型特化
std::numeric_limits<T>::is_specialized
static const bool is_specialized; | (C++11 前) | |
static constexpr bool is_specialized; | (C++11 起) |
std::numeric_limits<T>::is_specialized 的值对所有存在 std::numeric_limits 特化的 T
为 true 。
标准特化
T | std::numeric_limits<T>::is_specialized 的值 |
/* non-specialized */ | false |
bool | true |
char | true |
signed char | true |
unsigned char | true |
wchar_t | true |
char8_t | true |
char16_t | true |
char32_t | true |
short | true |
unsigned short | true |
int | true |
unsigned int | true |
long | true |
unsigned long | true |
long long | true |
unsigned long long | true |
float | true |
double | true |
long double | true |
调用示例
#include <iostream>
#include <string>
#include <limits>
struct SName
{
};
//偏特化
struct SPartSpec
{
};
namespace std
{
template<>
struct numeric_limits<SPartSpec>
{
static _GLIBCXX_USE_CONSTEXPR bool is_specialized = true;
static _GLIBCXX_USE_CONSTEXPR bool is_signed = true;
};
}
int main()
{
std::cout << std::boolalpha;
std::cout << "std::numeric_limits<bool>::is_specialized: "
<< std::numeric_limits<bool>::is_specialized << std::endl;
std::cout << "std::numeric_limits<char>::is_specialized: "
<< std::numeric_limits<char>::is_specialized << std::endl;
std::cout << "std::numeric_limits<signed char>::is_specialized: "
<< std::numeric_limits<signed char>::is_specialized << std::endl;
std::cout << "std::numeric_limits<unsigned char>::is_specialized: "
<< std::numeric_limits<unsigned char>::is_specialized << std::endl;
std::cout << "std::numeric_limits<wchar_t>::is_specialized: "
<< std::numeric_limits<wchar_t>::is_specialized << std::endl;
std::cout << "std::numeric_limits<char16_t>::is_specialized: "
<< std::numeric_limits<char16_t>::is_specialized << std::endl;
std::cout << "std::numeric_limits<char32_t>::is_specialized: "
<< std::numeric_limits<char32_t>::is_specialized << std::endl;
std::cout << "std::numeric_limits<short>::is_specialized: "
<< std::numeric_limits<short>::is_specialized << std::endl;
std::cout << "std::numeric_limits<unsigned short>::is_specialized: "
<< std::numeric_limits<unsigned short>::is_specialized << std::endl;
std::cout << "std::numeric_limits<int>::is_specialized: "
<< std::numeric_limits<int>::is_specialized << std::endl;
std::cout << "std::numeric_limits<unsigned int>::is_specialized: "
<< std::numeric_limits<unsigned int>::is_specialized << std::endl;
std::cout << "std::numeric_limits<long>::is_specialized: "
<< std::numeric_limits<long>::is_specialized << std::endl;
std::cout << "std::numeric_limits<unsigned long>::is_specialized: "
<< std::numeric_limits<unsigned long>::is_specialized << std::endl;
std::cout << "std::numeric_limits<long long>::is_specialized: "
<< std::numeric_limits<long long>::is_specialized << std::endl;
std::cout << "std::numeric_limits<unsigned long long>::is_specialized:"
<< std::numeric_limits<unsigned long long>::is_specialized << std::endl;
std::cout << "std::numeric_limits<float>::is_specialized: "
<< std::numeric_limits<float>::is_specialized << std::endl;
std::cout << "std::numeric_limits<double>::is_specialized: "
<< std::numeric_limits<double>::is_specialized << std::endl;
std::cout << "std::numeric_limits<long double>::is_specialized: "
<< std::numeric_limits<long double>::is_specialized << std::endl;
std::cout << "std::numeric_limits<std::string>::is_specialized: "
<< std::numeric_limits<std::string>::is_specialized << std::endl;
std::cout << "std::numeric_limits<SName>::is_specialized: "
<< std::numeric_limits<SName>::is_specialized << std::endl;
std::cout << "std::numeric_limits<SPartSpec>::is_specialized: "
<< std::numeric_limits<SPartSpec>::is_specialized << std::endl;
return 0;
}
输出
鉴别有符号类型
std::numeric_limits<T>::is_signed
static const bool is_signed; | (C++11 前) | |
static constexpr bool is_signed; | (C++11 起) |
std::numeric_limits<T>::is_signed 的值对所有有符号算数类型 T
为 true ,而对无符号类型为 false 。此常量对所有特化有意义。
标准特化
T | std::numeric_limits<T>::is_signed 的值 |
/* non-specialized */ | false |
bool | false |
char | 实现定义 |
signed char | true |
unsigned char | false |
wchar_t | 实现定义 |
char8_t | false |
char16_t | false |
char32_t | false |
short | true |
unsigned short | false |
int | true |
unsigned int | false |
long | true |
unsigned long | false |
long long | true |
unsigned long long | false |
float | true |
double | true |
long double | true |
调用示例
#include <iostream>
#include <string>
#include <limits>
struct SName
{
};
//偏特化
struct SPartSpec
{
};
namespace std
{
template<>
struct numeric_limits<SPartSpec>
{
static _GLIBCXX_USE_CONSTEXPR bool is_specialized = true;
static _GLIBCXX_USE_CONSTEXPR bool is_signed = true;
};
}
int main()
{
std::cout << std::boolalpha;
std::cout << "std::numeric_limits<bool>::is_signed: "
<< std::numeric_limits<bool>::is_signed << std::endl;
std::cout << "std::numeric_limits<char>::is_signed: "
<< std::numeric_limits<char>::is_signed << std::endl;
std::cout << "std::numeric_limits<signed char>::is_signed: "
<< std::numeric_limits<signed char>::is_signed << std::endl;
std::cout << "std::numeric_limits<unsigned char>::is_signed: "
<< std::numeric_limits<unsigned char>::is_signed << std::endl;
std::cout << "std::numeric_limits<wchar_t>::is_signed: "
<< std::numeric_limits<wchar_t>::is_signed << std::endl;
std::cout << "std::numeric_limits<char16_t>::is_signed: "
<< std::numeric_limits<char16_t>::is_signed << std::endl;
std::cout << "std::numeric_limits<char32_t>::is_signed: "
<< std::numeric_limits<char32_t>::is_signed << std::endl;
std::cout << "std::numeric_limits<short>::is_signed: "
<< std::numeric_limits<short>::is_signed << std::endl;
std::cout << "std::numeric_limits<unsigned short>::is_signed: "
<< std::numeric_limits<unsigned short>::is_signed << std::endl;
std::cout << "std::numeric_limits<int>::is_signed: "
<< std::numeric_limits<int>::is_signed << std::endl;
std::cout << "std::numeric_limits<unsigned int>::is_signed: "
<< std::numeric_limits<unsigned int>::is_signed << std::endl;
std::cout << "std::numeric_limits<long>::is_signed: "
<< std::numeric_limits<long>::is_signed << std::endl;
std::cout << "std::numeric_limits<unsigned long>::is_signed: "
<< std::numeric_limits<unsigned long>::is_signed << std::endl;
std::cout << "std::numeric_limits<long long>::is_signed: "
<< std::numeric_limits<long long>::is_signed << std::endl;
std::cout << "std::numeric_limits<unsigned long long>::is_signed: "
<< std::numeric_limits<unsigned long long>::is_signed << std::endl;
std::cout << "std::numeric_limits<float>::is_signed: "
<< std::numeric_limits<float>::is_signed << std::endl;
std::cout << "std::numeric_limits<double>::is_signed: "
<< std::numeric_limits<double>::is_signed << std::endl;
std::cout << "std::numeric_limits<long double>::is_signed: "
<< std::numeric_limits<long double>::is_signed << std::endl;
std::cout << "std::numeric_limits<std::string>::is_signed: "
<< std::numeric_limits<std::string>::is_signed << std::endl;
std::cout << "std::numeric_limits<SName>::is_signed: "
<< std::numeric_limits<SName>::is_signed << std::endl;
std::cout << "std::numeric_limits<SPartSpec>::is_signed: "
<< std::numeric_limits<SPartSpec>::is_signed << std::endl;
return 0;
}
输出
鉴别整数类型
std::numeric_limits<T>::is_integer
static const bool is_integer; | (C++11 前) | |
static constexpr bool is_integer; | (C++11 起) |
标准特化
T | std::numeric_limits<T>::is_integer 的值 |
/* non-specialized */ | false |
bool | true |
char | true |
signed char | true |
unsigned char | true |
wchar_t | true |
char8_t | true |
char16_t | true |
char32_t | true |
short | true |
unsigned short | true |
int | true |
unsigned int | true |
long | true |
unsigned long | true |
long long | true |
unsigned long long | true |
float | false |
double | false |
long double | false |
调用示例
#include <iostream>
#include <string>
#include <limits>
struct SName
{
};
//偏特化
struct SPartSpec
{
};
namespace std
{
template<>
struct numeric_limits<SPartSpec>
{
static _GLIBCXX_USE_CONSTEXPR bool is_specialized = true;
static _GLIBCXX_USE_CONSTEXPR bool is_signed = true;
static _GLIBCXX_USE_CONSTEXPR bool is_integer = true;
};
}
int main()
{
std::cout << std::boolalpha;
std::cout << "std::numeric_limits<bool>::is_integer: "
<< std::numeric_limits<bool>::is_integer << std::endl;
std::cout << "std::numeric_limits<char>::is_integer: "
<< std::numeric_limits<char>::is_integer << std::endl;
std::cout << "std::numeric_limits<signed char>::is_integer: "
<< std::numeric_limits<signed char>::is_integer << std::endl;
std::cout << "std::numeric_limits<unsigned char>::is_integer: "
<< std::numeric_limits<unsigned char>::is_integer << std::endl;
std::cout << "std::numeric_limits<wchar_t>::is_integer: "
<< std::numeric_limits<wchar_t>::is_integer << std::endl;
std::cout << "std::numeric_limits<char16_t>::is_integer: "
<< std::numeric_limits<char16_t>::is_integer << std::endl;
std::cout << "std::numeric_limits<char32_t>::is_integer: "
<< std::numeric_limits<char32_t>::is_integer << std::endl;
std::cout << "std::numeric_limits<short>::is_integer: "
<< std::numeric_limits<short>::is_integer << std::endl;
std::cout << "std::numeric_limits<unsigned short>::is_integer: "
<< std::numeric_limits<unsigned short>::is_integer << std::endl;
std::cout << "std::numeric_limits<int>::is_integer: "
<< std::numeric_limits<int>::is_integer << std::endl;
std::cout << "std::numeric_limits<unsigned int>::is_integer: "
<< std::numeric_limits<unsigned int>::is_integer << std::endl;
std::cout << "std::numeric_limits<long>::is_integer: "
<< std::numeric_limits<long>::is_integer << std::endl;
std::cout << "std::numeric_limits<unsigned long>::is_integer: "
<< std::numeric_limits<unsigned long>::is_integer << std::endl;
std::cout << "std::numeric_limits<long long>::is_integer: "
<< std::numeric_limits<long long>::is_integer << std::endl;
std::cout << "std::numeric_limits<unsigned long long>::is_integer: "
<< std::numeric_limits<unsigned long long>::is_integer << std::endl;
std::cout << "std::numeric_limits<float>::is_integer: "
<< std::numeric_limits<float>::is_integer << std::endl;
std::cout << "std::numeric_limits<double>::is_integer: "
<< std::numeric_limits<double>::is_integer << std::endl;
std::cout << "std::numeric_limits<long double>::is_integer: "
<< std::numeric_limits<long double>::is_integer << std::endl;
std::cout << "std::numeric_limits<std::string>::is_integer: "
<< std::numeric_limits<std::string>::is_integer << std::endl;
std::cout << "std::numeric_limits<SName>::is_integer: "
<< std::numeric_limits<SName>::is_integer << std::endl;
std::cout << "std::numeric_limits<SPartSpec>::is_integer: "
<< std::numeric_limits<SPartSpec>::is_integer << std::endl;
return 0;
}
输出
std::numeric_limits<bool>::is_integer: true
std::numeric_limits<char>::is_integer: true
std::numeric_limits<signed char>::is_integer: true
std::numeric_limits<unsigned char>::is_integer: true
std::numeric_limits<wchar_t>::is_integer: true
std::numeric_limits<char16_t>::is_integer: true
std::numeric_limits<char32_t>::is_integer: true
std::numeric_limits<short>::is_integer: true
std::numeric_limits<unsigned short>::is_integer: true
std::numeric_limits<int>::is_integer: true
std::numeric_limits<unsigned int>::is_integer: true
std::numeric_limits<long>::is_integer: true
std::numeric_limits<unsigned long>::is_integer: true
std::numeric_limits<long long>::is_integer: true
std::numeric_limits<unsigned long long>::is_integer: true
std::numeric_limits<float>::is_integer: false
std::numeric_limits<double>::is_integer: false
std::numeric_limits<long double>::is_integer: false
std::numeric_limits<std::string>::is_integer: false
std::numeric_limits<SName>::is_integer: false
std::numeric_limits<SPartSpec>::is_integer: true