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 。

标准特化

|-------------------------|---------------------------------------------|
| 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
相关推荐
再卷也是菜2 小时前
C++篇(21)图
数据结构·c++·算法
星轨初途2 小时前
C++入门(算法竞赛类)
c++·经验分享·笔记·算法
Bona Sun2 小时前
单片机手搓掌上游戏机(十三)—pico运行fc模拟器之硬件准备
c语言·c++·单片机·游戏机
Bona Sun2 小时前
单片机手搓掌上游戏机(十八)—pico运行fc模拟器之更大屏幕
c语言·c++·单片机·游戏机
chenyuhao20242 小时前
MySQL索引特性
开发语言·数据库·c++·后端·mysql
小龙报3 小时前
【算法通关指南:数据结构和算法篇 】队列相关算法题:3.海港
数据结构·c++·算法·贪心算法·创业创新·学习方法·visual studio
辞旧 lekkk3 小时前
【c++】封装红黑树实现mymap和myset
c++·学习·算法·萌新
星轨初途3 小时前
C++的输入输出(上)(算法竞赛类)
开发语言·c++·经验分享·笔记·算法
极地星光4 小时前
Qt/C++ 单例模式深度解析:饿汉式与懒汉式实战指南
c++·qt·单例模式
yuuki2332334 小时前
【C++】类和对象(上)
c++·后端·算法