C++标准模板(STL)- 类型支持 (数值极限,is_specialized,is_signed,is_integer)

数值极限


定义于头文件 <limits>

定义于头文件 <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 。

标准特化

Tstd::numeric_limits<T>::is_specialized 的值
/* non-specialized */false
booltrue
chartrue
signed chartrue
unsigned chartrue
wchar_ttrue
char8_ttrue
char16_ttrue
char32_ttrue
shorttrue
unsigned shorttrue
inttrue
unsigned inttrue
longtrue
unsigned longtrue
long longtrue
unsigned long longtrue
floattrue
doubletrue
long doubletrue

调用示例

#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 。此常量对所有特化有意义。

标准特化

Tstd::numeric_limits<T>::is_signed 的值
/* non-specialized */false
boolfalse
char实现定义
signed chartrue
unsigned charfalse
wchar_t实现定义
char8_tfalse
char16_tfalse
char32_tfalse
shorttrue
unsigned shortfalse
inttrue
unsigned intfalse
longtrue
unsigned longfalse
long longtrue
unsigned long longfalse
floattrue
doubletrue
long doubletrue

调用示例 

#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 起)

标准特化

Tstd::numeric_limits<T>::is_integer 的值
/* non-specialized */false
booltrue
chartrue
signed chartrue
unsigned chartrue
wchar_ttrue
char8_ttrue
char16_ttrue
char32_ttrue
shorttrue
unsigned shorttrue
inttrue
unsigned inttrue
longtrue
unsigned longtrue
long longtrue
unsigned long longtrue
floatfalse
doublefalse
long doublefalse

调用示例 

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值