Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add operator< to , to allow use as a map key #3680

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions include/nlohmann/detail/json_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,15 @@ class json_pointer
{
return *this == json_pointer(rhs);
}

/// @brief compares JSON pointer and string for less-than
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
friend bool operator<(json_pointer<RefStringTypeLhs> const& lhs,
json_pointer<RefStringTypeRhs> const& rhs) noexcept
{
return lhs.reference_tokens < rhs.reference_tokens;
}

#else
/// @brief compares two JSON pointers for equality
/// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
Expand Down Expand Up @@ -904,6 +913,12 @@ class json_pointer
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator!=(const StringType& lhs,
const json_pointer<RefStringTypeRhs>& rhs);

/// @brief compares string and JSON pointer for less-than
template<typename RefStringTypeRhs, typename StringType>
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator<(const StringType& lhs,
const json_pointer<RefStringTypeRhs>& rhs);
#endif

private:
Expand Down Expand Up @@ -958,6 +973,13 @@ inline bool operator!=(const StringType& lhs,
{
return !(lhs == rhs);
}

template<typename RefStringTypeLhs, typename RefStringTypeRhs>
inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
const json_pointer<RefStringTypeRhs>& rhs) noexcept
{
return lhs.reference_tokens < rhs.reference_tokens;
}
#endif

NLOHMANN_JSON_NAMESPACE_END
22 changes: 22 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14464,6 +14464,15 @@ class json_pointer
{
return *this == json_pointer(rhs);
}

/// @brief compares JSON pointer and string for less-than
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
friend bool operator<(json_pointer<RefStringTypeLhs> const& lhs,
json_pointer<RefStringTypeRhs> const& rhs) noexcept
{
return lhs.reference_tokens < rhs.reference_tokens;
}

#else
/// @brief compares two JSON pointers for equality
/// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
Expand Down Expand Up @@ -14506,6 +14515,12 @@ class json_pointer
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator!=(const StringType& lhs,
const json_pointer<RefStringTypeRhs>& rhs);

/// @brief compares string and JSON pointer for less-than
template<typename RefStringTypeRhs, typename StringType>
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator<(const StringType& lhs,
const json_pointer<RefStringTypeRhs>& rhs);
#endif

private:
Expand Down Expand Up @@ -14560,6 +14575,13 @@ inline bool operator!=(const StringType& lhs,
{
return !(lhs == rhs);
}

template<typename RefStringTypeLhs, typename RefStringTypeRhs>
inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
const json_pointer<RefStringTypeRhs>& rhs) noexcept
{
return lhs.reference_tokens < rhs.reference_tokens;
}
#endif

NLOHMANN_JSON_NAMESPACE_END
Expand Down