ev-nlohmann-integer-sequence-tuple-002¶
| 字段 | 值 |
|---|---|
| 项目 | nlohmann/json |
| 固定 commit | 55f93686c015 |
| 路径 | single_include/nlohmann/json.hpp |
| 行段 | L5230-L5234 |
| 特性 | std::integer_sequence |
| 许可证 | MIT |
| 代码来源 | 项目源码 |
| 级别 | manual |
| 审核 | zyuhao7 @ 2026-11-03 |
| 测试代码 | 否 |
| 发现方式 | manual |
用法¶
用 index_sequence 把 JSON 数组下标展开为 tuple 元素
为什么典型¶
反序列化 tuple 时,第 i 个 JSON 元素对应第 i 个 tuple 字段;index_sequence 让编译器生成一整套带序号的 get 调用,无需手写每个下标。
摘录¶
template<typename BasicJsonType, typename... Args, std::size_t... Idx>
std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
{
return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
}
备注¶
生产单头文件;外层用 index_sequence_for