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
相关推荐
捕鲸叉5 小时前
创建线程时传递参数给线程
开发语言·c++·算法
A charmer5 小时前
【C++】vector 类深度解析:探索动态数组的奥秘
开发语言·c++·算法
Peter_chq5 小时前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端
青花瓷7 小时前
C++__XCode工程中Debug版本库向Release版本库的切换
c++·xcode
幺零九零零8 小时前
【C++】socket套接字编程
linux·服务器·网络·c++
捕鲸叉8 小时前
MVC(Model-View-Controller)模式概述
开发语言·c++·设计模式
Dola_Pan9 小时前
C++算法和竞赛:哈希算法、动态规划DP算法、贪心算法、博弈算法
c++·算法·哈希算法
yanlou2339 小时前
KMP算法,next数组详解(c++)
开发语言·c++·kmp算法
小林熬夜学编程9 小时前
【Linux系统编程】第四十一弹---线程深度解析:从地址空间到多线程实践
linux·c语言·开发语言·c++·算法
阿洵Rain10 小时前
【C++】哈希
数据结构·c++·算法·list·哈希算法