文档目录

C++20 新增标准库工具

目标:掌握 C++20 新增的标准库实用工具。

  • 4.1 std::span<T>(连续内存视图)

    • 替代 (T* data, size_t len) 的函数参数模式。
    • 动态 extent 与静态 extent(std::span<int, 4>)的编译期优化。
  • 4.2 std::format(类型安全格式化)

    • 对比 printf 的类型不安全与 ostringstream 的性能低下。
    • 编译期格式字符串检查。
    • 自定义类型的 format 格式化器特化。
  • 4.3 std::source_location(源码位置)

    • 替代 __FILE__、__LINE__ 宏的现代方式。
    • 日志库、断言宏中的标准用法。
  • 4.4 std::endian 与 std::byteswap

    • 编译期检测大/小端序。
    • 网络字节序转换的标准做法。
  • 4.5 std::bit_cast(类型双关安全转换)

    • 替代 reinterpret_cast/memcpy 的未定义行为方式。
    • 编译时 constexpr 位转换。