ev-json-concepts-scalar-spaceship-003¶
| 字段 | 值 |
|---|---|
| 项目 | nlohmann/json |
| 固定 commit | 55f93686c015 |
| 路径 | include/nlohmann/json.hpp |
| 行段 | L3756-L3763 |
| 特性 | concepts 与 requires |
| 许可证 | MIT |
| 代码来源 | 项目源码 |
| 级别 | manual |
| 审核 | zyuhao7 @ 2026-10-24 |
| 测试代码 | 否 |
| 发现方式 | regex |
用法¶
同一标量约束复用于三路比较重载
为什么典型¶
相关运算符共享一致的约束,避免一个重载接受某类型而另一个拒绝,减少条件编译分支的漂移。
摘录¶
/// @brief comparison: 3-way
/// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
template<typename ScalarType>
requires std::is_scalar_v<ScalarType>
std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
{
return *this <=> basic_json(rhs); // *NOPAD*
}
备注¶
生产路径;与相等比较共享同一约束。