ev-fmt-bit-cast-fallback-001¶
| 字段 | 值 |
|---|---|
| 项目 | fmtlib/fmt |
| 固定 commit | 1be298e1bd68 |
| 路径 | include/fmt/format.h |
| 行段 | L266-L276 |
| 特性 | std::bit_cast |
| 许可证 | MIT |
| 代码来源 | 项目源码 |
| 级别 | manual |
| 审核 | zyuhao7 @ 2026-10-26 |
| 测试代码 | 否 |
| 发现方式 | regex |
用法¶
在常量求值时调用 std::bit_cast,运行期回退到 memcpy
为什么典型¶
兼容层既要支持 C++17,又要在 C++20 的常量求值中保持合法;特性宏把标准实现嵌入回退路径。
摘录¶
// Implementation of std::bit_cast for pre-C++20.
template <typename To, typename From, FMT_ENABLE_IF(sizeof(To) == sizeof(From))>
FMT_CONSTEXPR20 auto bit_cast(const From& from) -> To {
#ifdef __cpp_lib_bit_cast
if (is_constant_evaluated()) return std::bit_cast<To>(from);
#endif
auto to = To();
// The cast suppresses a bogus -Wclass-memaccess on GCC.
memcpy(static_cast<void*>(&to), &from, sizeof(to));
return to;
}
备注¶
生产路径;__cpp_lib_bit_cast 可用时在常量求值分支使用标准实现。