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
相关推荐
唐诺4 小时前
几种广泛使用的 C++ 编译器
c++·编译器
冷眼看人间恩怨5 小时前
【Qt笔记】QDockWidget控件详解
c++·笔记·qt·qdockwidget
红龙创客5 小时前
某狐畅游24校招-C++开发岗笔试(单选题)
开发语言·c++
Lenyiin5 小时前
第146场双周赛:统计符合条件长度为3的子数组数目、统计异或值为给定值的路径数目、判断网格图能否被切割成块、唯一中间众数子序列 Ⅰ
c++·算法·leetcode·周赛·lenyiin
yuanbenshidiaos7 小时前
c++---------数据类型
java·jvm·c++
十年一梦实验室7 小时前
【C++】sophus : sim_details.hpp 实现了矩阵函数 W、其导数,以及其逆 (十七)
开发语言·c++·线性代数·矩阵
taoyong0017 小时前
代码随想录算法训练营第十一天-239.滑动窗口最大值
c++·算法
这是我588 小时前
C++打小怪游戏
c++·其他·游戏·visual studio·小怪·大型·怪物
fpcc8 小时前
跟我学c++中级篇——C++中的缓存利用
c++·缓存
呆萌很8 小时前
C++ 集合 list 使用
c++