fix #414 - comparing to 0 literal
Overload comparison operators for all types that could be converted to nullptr.
This commit is contained in:
parent
9755cc75b1
commit
6198439f59
3 changed files with 490 additions and 996 deletions
1401
src/json.hpp
1401
src/json.hpp
File diff suppressed because it is too large
Load diff
|
@ -5625,36 +5625,23 @@ class basic_json
|
|||
return false;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief comparison: equal
|
||||
|
||||
The functions compares the given JSON value against a null pointer. As the
|
||||
null pointer can be used to initialize a JSON value to null, a comparison
|
||||
of JSON value @a v with a null pointer should be equivalent to call
|
||||
`v.is_null()`.
|
||||
|
||||
@param[in] v JSON value to consider
|
||||
@return whether @a v is null
|
||||
|
||||
@complexity Constant.
|
||||
|
||||
@liveexample{The example compares several JSON types to the null pointer.
|
||||
,operator__equal__nullptr_t}
|
||||
|
||||
@since version 1.0.0
|
||||
/*!@brief comparison: equal
|
||||
@copydoc operator==(const_reference, const_reference)
|
||||
*/
|
||||
friend bool operator==(const_reference v, std::nullptr_t) noexcept
|
||||
{
|
||||
return v.is_null();
|
||||
template<typename ScalarType, typename std::enable_if<
|
||||
std::is_scalar<ScalarType>::value, int>::type = 0>
|
||||
friend bool operator==(const_reference lhs, const ScalarType rhs) noexcept {
|
||||
return (lhs == basic_json(rhs));
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief comparison: equal
|
||||
@copydoc operator==(const_reference, std::nullptr_t)
|
||||
@copydoc operator==(const_reference, const_reference)
|
||||
*/
|
||||
friend bool operator==(std::nullptr_t, const_reference v) noexcept
|
||||
{
|
||||
return v.is_null();
|
||||
template<typename ScalarType, typename std::enable_if<
|
||||
std::is_scalar<ScalarType>::value, int>::type = 0>
|
||||
friend bool operator==(const ScalarType lhs, const_reference rhs) noexcept {
|
||||
return (basic_json(lhs) == rhs);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
@ -5680,34 +5667,24 @@ class basic_json
|
|||
|
||||
/*!
|
||||
@brief comparison: not equal
|
||||
|
||||
The functions compares the given JSON value against a null pointer. As the
|
||||
null pointer can be used to initialize a JSON value to null, a comparison
|
||||
of JSON value @a v with a null pointer should be equivalent to call
|
||||
`not v.is_null()`.
|
||||
|
||||
@param[in] v JSON value to consider
|
||||
@return whether @a v is not null
|
||||
|
||||
@complexity Constant.
|
||||
|
||||
@liveexample{The example compares several JSON types to the null pointer.
|
||||
,operator__notequal__nullptr_t}
|
||||
|
||||
@since version 1.0.0
|
||||
@copydoc operator!=(const_reference, const_reference)
|
||||
*/
|
||||
friend bool operator!=(const_reference v, std::nullptr_t) noexcept
|
||||
template<typename ScalarType, typename std::enable_if<
|
||||
std::is_scalar<ScalarType>::value, int>::type = 0>
|
||||
friend bool operator!=(const_reference lhs, const ScalarType rhs) noexcept
|
||||
{
|
||||
return not v.is_null();
|
||||
return (lhs != basic_json(rhs));
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief comparison: not equal
|
||||
@copydoc operator!=(const_reference, std::nullptr_t)
|
||||
@copydoc operator!=(const_reference, const_reference)
|
||||
*/
|
||||
friend bool operator!=(std::nullptr_t, const_reference v) noexcept
|
||||
template<typename ScalarType, typename std::enable_if<
|
||||
std::is_scalar<ScalarType>::value, int>::type = 0>
|
||||
friend bool operator!=(const ScalarType lhs, const_reference rhs) noexcept
|
||||
{
|
||||
return not v.is_null();
|
||||
return (basic_json(lhs) != rhs);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
|
@ -663,4 +663,22 @@ TEST_CASE("regression tests")
|
|||
std::vector<uint8_t> vec3 {0xbf, 0x61, 0x61, 0x01};
|
||||
CHECK_THROWS_AS(json::from_cbor(vec3), std::out_of_range);
|
||||
}
|
||||
|
||||
SECTION("issue #414 - compare with literal 0)")
|
||||
{
|
||||
#define CHECK_TYPE(v) \
|
||||
CHECK((json(v) == v));\
|
||||
CHECK((v == json(v)));\
|
||||
CHECK_FALSE((json(v) != v));\
|
||||
CHECK_FALSE((v != json(v)));
|
||||
|
||||
CHECK_TYPE(nullptr);
|
||||
CHECK_TYPE(0);
|
||||
CHECK_TYPE(0u);
|
||||
CHECK_TYPE(0L);
|
||||
CHECK_TYPE(0.0);
|
||||
CHECK_TYPE("");
|
||||
|
||||
#undef CHECK_TYPE
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue