1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-10 22:00:59 +03:00

Allow comparing default initialized iterators (#4512)

This commit is contained in:
Niels Lohmann
2024-11-26 06:35:14 +01:00
committed by GitHub
parent ee32bfc1c2
commit 3d6c664de2
5 changed files with 89 additions and 41 deletions

View File

@ -13439,7 +13439,7 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci
/*!
@brief comparison: equal
@pre The iterator is initialized; i.e. `m_object != nullptr`.
@pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
*/
template < typename IterImpl, detail::enable_if_t < (std::is_same<IterImpl, iter_impl>::value || std::is_same<IterImpl, other_iter_impl>::value), std::nullptr_t > = nullptr >
bool operator==(const IterImpl& other) const
@ -13450,7 +13450,11 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci
JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
}
JSON_ASSERT(m_object != nullptr);
// value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493
if (m_object == nullptr)
{
return true;
}
switch (m_object->m_data.m_type)
{
@ -13475,7 +13479,7 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci
/*!
@brief comparison: not equal
@pre The iterator is initialized; i.e. `m_object != nullptr`.
@pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
*/
template < typename IterImpl, detail::enable_if_t < (std::is_same<IterImpl, iter_impl>::value || std::is_same<IterImpl, other_iter_impl>::value), std::nullptr_t > = nullptr >
bool operator!=(const IterImpl& other) const
@ -13485,7 +13489,7 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci
/*!
@brief comparison: smaller
@pre The iterator is initialized; i.e. `m_object != nullptr`.
@pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
*/
bool operator<(const iter_impl& other) const
{
@ -13495,7 +13499,12 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci
JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
}
JSON_ASSERT(m_object != nullptr);
// value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493
if (m_object == nullptr)
{
// the iterators are both value-initialized and are to be considered equal, but this function checks for smaller, so we return false
return false;
}
switch (m_object->m_data.m_type)
{
@ -13520,7 +13529,7 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci
/*!
@brief comparison: less than or equal
@pre The iterator is initialized; i.e. `m_object != nullptr`.
@pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
*/
bool operator<=(const iter_impl& other) const
{
@ -13529,7 +13538,7 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci
/*!
@brief comparison: greater than
@pre The iterator is initialized; i.e. `m_object != nullptr`.
@pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
*/
bool operator>(const iter_impl& other) const
{
@ -13538,7 +13547,7 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci
/*!
@brief comparison: greater than or equal
@pre The iterator is initialized; i.e. `m_object != nullptr`.
@pre (1) The iterator is initialized; i.e. `m_object != nullptr`, or (2) both iterators are value-initialized.
*/
bool operator>=(const iter_impl& other) const
{