ev-json-concepts-scalar-eq-002¶
| 字段 | 值 |
|---|---|
| 项目 | nlohmann/json |
| 固定 commit | 55f93686c015 |
| 路径 | include/nlohmann/json.hpp |
| 行段 | L3723-L3730 |
| 特性 | concepts 与 requires |
| 许可证 | MIT |
| 代码来源 | 项目源码 |
| 级别 | manual |
| 审核 | zyuhao7 @ 2026-10-24 |
| 测试代码 | 否 |
| 发现方式 | regex |
用法¶
用 requires 限制标量相等比较重载
为什么典型¶
约束写在声明处,标量重载只在 ScalarType 满足条件时进入候选集,避免与容器重载竞争。
摘录¶
/// @brief comparison: equal
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
template<typename ScalarType>
requires std::is_scalar_v<ScalarType>
bool operator==(ScalarType rhs) const noexcept
{
return *this == basic_json(rhs);
}
备注¶
生产路径;requires 子句直接使用布尔变量模板。