• C++标准模板(STL)- 类型支持 ()


    对象、引用、函数(包括函数模板特化)和表达式具有称为类型的性质,它限制了对这些实体所容许的操作,并给原本寻常的位序列提供了语义含义。

    附加性基本类型及宏

    实现定义的空指针常量

    NULL

    定义于头文件

    定义于头文件

    定义于头文件

    定义于头文件

    定义于头文件

    定义于头文件

    定义于头文件

    #define NULL /*implementation-defined*/

    NULL 是实现定义的空指针常量,可为

    求值为零的整数类型字面常量表达式右值

    (C++11 前)

    零值整数字面量,或为 std::nullptr_t 类型纯右值

    (C++11 起)

    空指针常量可以隐式转换为任何指针类型;这种转换结果是该类型的空指针值。若空指针常量拥有整数类型,它亦可转换为 std::nullptr_t 类型纯右值。

    可能的实现

    1. #define NULL 0
    2. // C++11
    3. #define NULL nullptr

    注意

    C 中,宏 NULL 可以拥有类型 void* ,但这在 C++ 中不允许。

    调用示例

    1. #include <cstddef>
    2. #include <type_traits>
    3. #include <iostream>
    4. class S;
    5. int main()
    6. {
    7. int* p = NULL;
    8. int* p2 = static_cast<std::nullptr_t>(NULL);
    9. void(*f)(int) = NULL;
    10. int S::*mp = NULL;
    11. void(S::*mfp)(int) = NULL;
    12. if (std::is_same<decltype(NULL), std::nullptr_t>::value)
    13. {
    14. std::cout << "NULL implemented with type std::nullptr_t" << std::endl;
    15. }
    16. else
    17. {
    18. std::cout << "NULL implemented using an integral type" << std::endl;
    19. }
    20. return 0;
    21. }

     输出

    具有不小于任何基础类型的内存对齐需求的平凡类型

    std::max_align_t

    定义于头文件

    typedef /*implementation-defined*/ max_align_t;

    (C++11 起)

     注意

    分配函数,如 std::malloc 所返回的指针,适于为任何对象对齐,这表示其对齐至少与 std::max_align_t 一样严格。

    std::max_align_t 通常是最大标量类型的同意词,在大多数平台上是 long double ,而其对齐要求是 8 或 16 。

    调用示例

    1. #include <iostream>
    2. #include <cstddef>
    3. int main()
    4. {
    5. std::cout << "alignof(std::max_align_t): "
    6. << alignof(std::max_align_t) << std::endl;
    7. return 0;
    8. }

    输出

    从标准布局类型的起始到其指定成员的字节偏移量

    offsetof

    定义于头文件

    #define offsetof(type, member) /*implementation-defined*/

    offsetof 展开成 std::size_t 类型的整数常量表达式,其值是从指定类型对象开始到其指定成员的字节数偏移,若有填充字节则包括之。

    type 不是标准布局类型,则行为未定义 (C++17 前)条件性支持 offsetof 宏的使用 (C++17 起)。

    member 是 static 成员或成员函数,则行为未定义。

    标准布局类型的首个成员的偏移始终是零(空基类优化是强制的)。

    表达式 offsetof(type, member) 决不依赖类型,而且它依赖值当且仅当 type 为依赖的。

    异常

    offsetof 不抛出异常;表达式 noexcept(offsetof(type, member)) 始终求值为 true 。

    注意

    offsetof 不能以标准 C++ 实现,并要求编译器支持: GCCLLVM

     调用示例

    1. #include <iostream>
    2. #include <cstddef>
    3. struct S
    4. {
    5. char c;
    6. double d;
    7. };
    8. int main()
    9. {
    10. std::cout << "the first element is at offset "
    11. << offsetof(S, c) << std::endl
    12. << "the double is at offset "
    13. << offsetof(S, d) << std::endl;
    14. return 0;
    15. }

    输出

  • 相关阅读:
    三线表的制作
    程序员必知必会的TCP/IP,业内大佬给讲明白了
    【21天python打卡】第4天 基础语法(2)
    数据挖掘实验:使用 Hadoop 实现 WordCount 应用
    【算法题】2864. 最大二进制奇数
    负载均衡 - F5
    项目总结-新增商品-Pagehelper插件分页查询
    c语言 static
    SpringBoot+vue实现前后端分离的摄影跟拍预定管理系统
    Kubernetes 笔记 / 任务 / 管理集群 / 用 kubeadm 管理集群 / 配置一个 cgroup 驱动
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/133430404