ev-fmt-if-constexpr-named-arg-002¶
| 字段 | 值 |
|---|---|
| 项目 | fmtlib/fmt |
| 固定 commit | 1be298e1bd68 |
| 路径 | include/fmt/compile.h |
| 行段 | L157-L166 |
| 特性 | if constexpr |
| 许可证 | MIT |
| 代码来源 | 项目源码 |
| 级别 | manual |
| 审核 | zyuhao7 @ 2026-10-24 |
| 测试代码 | 否 |
| 发现方式 | tree-sitter |
用法¶
按 is_named_arg 在编译期选择读取 .value 或原对象
为什么典型¶
用类型特征在编译期分派,避免运行期分支,且丢弃分支不会对普通参数生成无效访问。
摘录¶
// This ensures that the argument type is convertible to `const T&`.
template <typename T, int N, typename... Args>
constexpr auto get_arg_checked(const Args&... args) -> const T& {
const auto& arg = detail::get<N>(args...);
if constexpr (detail::is_named_arg<remove_cvref_t<decltype(arg)>>()) {
return arg.value;
} else {
return arg;
}
}
备注¶
生产路径;固定 commit 已由 tools/validate.py --check-corpus 校验。