ev-mold-tuple-name-type-002¶
| 字段 | 值 |
|---|---|
| 项目 | rui314/mold |
| 固定 commit | 27162691b71a |
| 路径 | src/gdb-index.cc |
| 行段 | L132-L135 |
| 特性 | std::tuple |
| 许可证 | MIT |
| 代码来源 | 项目源码 |
| 级别 | manual |
| 审核 | zyuhao7 @ 2026-11-03 |
| 测试代码 | 否 |
| 发现方式 | manual |
用法¶
用 tuple 的字典序比较实现多字段排序键
为什么典型¶
比较多个成员时,把字段装进 tuple 后直接用 operator<,避免手写逐字段的短路比较并保持字段顺序清晰。
摘录¶
bool operator<(const NameType &other) const {
return std::tuple(hash, type, name) <
std::tuple(other.hash, other.type, other.name);
}
备注¶
生产路径;CTAD 让 std::tuple(...) 省略模板实参,tuple 比较按元素顺序短路,正好对应多字段排序需求。