跳到主要内容

7. language (语言核心支持)

提供对 C++ 语言核心功能的支持。头文件: <typeinfo>, <type_traits>, <new>, <limits>, <climits>, <cfloat>, <cstddef>, <cstdint>。


class type_info

类型信息类。

  • 输入: 无
  • 输出: 类型信息对象
  • 注意: 由 typeid 运算符返回; 不可复制

const char* name() const

获取类型名称。

  • 输入: 无
  • 输出: const char* — 实现定义的名称

size_t hash_code() const noexcept

获取哈希码。

  • 输入: 无
  • 输出: size_t
  • 注意: C++11

class bad_typeid : public exception

typeid 空指针异常。

  • 输入: 无
  • 输出: 异常对象

class bad_cast : public exception

dynamic_cast 失败异常。

  • 输入: 无
  • 输出: 异常对象

7.1 new (动态内存)​


void* operator new(size_t size)

分配内存。

  • 输入: size — 字节数
  • 输出: void* — 内存地址
  • 注意: 失败抛出 bad_alloc

void* operator new(size_t size, const std::nothrow_t&) noexcept

不抛异常的分配。

  • 输入: size — 字节数
  • 输出: void* — 内存地址或 nullptr

void operator delete(void* ptr) noexcept

释放内存。

  • 输入: ptr — 内存地址
  • 输出: void

void* operator new[](size_t size)

数组分配。

  • 输入: size — 字节数
  • 输出: void*

void operator delete[](void* ptr) noexcept

数组释放。

  • 输入: ptr — 内存地址
  • 输出: void

class bad_alloc : public exception

内存分配失败异常。

  • 输入: 无
  • 输出: 异常对象

class bad_array_new_length : public bad_alloc

数组长度无效异常。

  • 输入: 无
  • 输出: 异常对象
  • 注意: C++11

7.2 limits (数值极限)​


template <class T> class numeric_limits

数值类型极限。

  • 输入: T — 算术类型
  • 输出: 极限值

static constexpr T min() noexcept

最小值。

  • 输入: 无
  • 输出: T — 最小值

static constexpr T max() noexcept

最大值。

  • 输入: 无
  • 输出: T — 最大值

static constexpr T lowest() noexcept

最低值(浮点)。

  • 输入: 无
  • 输出: T — 最低值
  • 注意: C++11; 对浮点为 -max()

static constexpr int digits

有效位数。

  • 输入: 无
  • 输出: int

static constexpr T epsilon() noexcept

机器 epsilon。

  • 输入: 无
  • 输出: T — 1 + epsilon > 1 的最小值

static constexpr T infinity() noexcept

无穷大。

  • 输入: 无
  • 输出: T

static constexpr T quiet_NaN() noexcept

静默 NaN。

  • 输入: 无
  • 输出: T

static constexpr bool has_infinity / has_quiet_NaN

是否有无穷大/NaN。

  • 输入: 无
  • 输出: bool

static constexpr int radix

基数。

  • 输入: 无
  • 输出: int — 2(二进制)或 10(十进制)

7.3 type_traits (类型特征)​


template <class T, T v> struct integral_constant

编译期常量包装。

  • 输入: T — 类型; v — 值
  • 输出: 含 value 成员的类型

using true_type = integral_constant<bool, true>

真常量。

  • 输入: 无
  • 输出: true_type

using false_type = integral_constant<bool, false>

假常量。

  • 输入: 无
  • 输出: false_type

template <class T> struct is_void / is_integral / is_floating_point / is_array / is_pointer / is_reference / is_enum / is_union / is_class / is_function

主要类型分类。

  • 输入: T
  • 输出: bool
  • 注意: 含 _v 变量模板(C++17)

template <class T> struct is_arithmetic / is_fundamental / is_scalar / is_object / is_compound / is_member_pointer

复合类型分类。

  • 输入: T
  • 输出: bool

template <class T> struct remove_const / remove_volatile / remove_cv / add_const / add_volatile / add_cv

cv 变换。

  • 输入: T
  • 输出: 变换后类型

template <class T> struct remove_reference / add_lvalue_reference / add_rvalue_reference

引用变换。

  • 输入: T
  • 输出: 变换后类型

template <class T> struct remove_pointer / add_pointer

指针变换。

  • 输入: T
  • 输出: 变换后类型

template <class T> struct make_signed / make_unsigned

有符号/无符号转换。

  • 输入: T — 整数类型
  • 输出: 转换后的类型

template <class T> struct remove_extent / remove_all_extents

移除数组维度。

  • 输入: T
  • 输出: 元素类型

template <class T, class U> struct is_same

是否同一类型。

  • 输入: T, U
  • 输出: bool

template <class Base, class Derived> struct is_base_of

是否基类。

  • 输入: Base, Derived
  • 输出: bool

template <class From, class To> struct is_convertible

是否可隐式转换。

  • 输入: From, To
  • 输出: bool

template <class T, class... Args> struct is_constructible / is_default_constructible / is_copy_constructible / is_move_constructible

构造属性。

  • 输入: T, Args...
  • 输出: bool

template <class T, class U> struct is_assignable / is_copy_assignable / is_move_assignable

赋值属性。

  • 输入: T, U
  • 输出: bool

template <class T> struct is_destructible / is_trivially_destructible / is_nothrow_destructible

析构属性。

  • 输入: T
  • 输出: bool

template <class T> struct is_trivially_copyable

是否平凡可复制。

  • 输入: T
  • 输出: bool
  • 注意: 可用 memcpy 安全复制

template <class T> struct is_standard_layout

是否标准布局。

  • 输入: T
  • 输出: bool

template <class T> struct is_empty / is_polymorphic / is_abstract / is_final

类型属性。

  • 输入: T
  • 输出: bool

template <bool B, class T = void> struct enable_if

条件启用类型。

  • 输入: B, T
  • 输出: type = T(若 B 为 true)
  • 注意: SFINAE 核心工具

template <bool B, class T, class F> struct conditional

条件选择类型。

  • 输入: B, T, F
  • 输出: type = T(若 B 为 true)否则 F

template <class T> struct decay

退化类型。

  • 输入: T
  • 输出: 退化后类型

template <class... T> struct common_type

公共类型。

  • 输入: 类型列表
  • 输出: 公共类型

template <class T> struct underlying_type

枚举底层类型。

  • 输入: T
  • 输出: 底层整数类型