Fix 'const' qualifier on bool& has no effect (#3678)

* Fix 'const' qualifier on bool& has no effect

Thanks, @georgthegreat, for pointing out this issue.

* Extend std::vector<bool> unit test
This commit is contained in:
Florian Albrechtskirchinger 2022-08-07 13:50:08 +02:00 committed by GitHub
parent bfbe774d8f
commit f1e34070d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 7 deletions

View File

@ -267,9 +267,15 @@ inline void to_json(BasicJsonType& j, T b) noexcept
external_constructor<value_t::boolean>::construct(j, b); external_constructor<value_t::boolean>::construct(j, b);
} }
template<typename BasicJsonType, template < typename BasicJsonType, typename BoolRef,
enable_if_t<std::is_convertible<const std::vector<bool>::reference&, typename BasicJsonType::boolean_t>::value, int> = 0> enable_if_t <
inline void to_json(BasicJsonType& j, const std::vector<bool>::reference& b) noexcept ((std::is_same<std::vector<bool>::reference, BoolRef>::value
&& !std::is_same <std::vector<bool>::reference, typename BasicJsonType::boolean_t&>::value)
|| (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
&& !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
typename BasicJsonType::boolean_t >::value))
&& std::is_convertible<const BoolRef&, typename BasicJsonType::boolean_t>::value, int > = 0 >
inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept
{ {
external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b)); external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
} }

View File

@ -5500,9 +5500,15 @@ inline void to_json(BasicJsonType& j, T b) noexcept
external_constructor<value_t::boolean>::construct(j, b); external_constructor<value_t::boolean>::construct(j, b);
} }
template<typename BasicJsonType, template < typename BasicJsonType, typename BoolRef,
enable_if_t<std::is_convertible<const std::vector<bool>::reference&, typename BasicJsonType::boolean_t>::value, int> = 0> enable_if_t <
inline void to_json(BasicJsonType& j, const std::vector<bool>::reference& b) noexcept ((std::is_same<std::vector<bool>::reference, BoolRef>::value
&& !std::is_same <std::vector<bool>::reference, typename BasicJsonType::boolean_t&>::value)
|| (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
&& !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
typename BasicJsonType::boolean_t >::value))
&& std::is_convertible<const BoolRef&, typename BasicJsonType::boolean_t>::value, int > = 0 >
inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept
{ {
external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b)); external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
} }

View File

@ -454,10 +454,19 @@ TEST_CASE("constructors")
CHECK(j.type() == json::value_t::boolean); CHECK(j.type() == json::value_t::boolean);
} }
SECTION("from std::vector<bool>::refrence") SECTION("from std::vector<bool>::reference")
{ {
std::vector<bool> v{true}; std::vector<bool> v{true};
json j(v[0]); json j(v[0]);
CHECK(std::is_same<decltype(v[0]), std::vector<bool>::reference>::value);
CHECK(j.type() == json::value_t::boolean);
}
SECTION("from std::vector<bool>::const_reference")
{
const std::vector<bool> v{true};
json j(v[0]);
CHECK(std::is_same<decltype(v[0]), std::vector<bool>::const_reference>::value);
CHECK(j.type() == json::value_t::boolean); CHECK(j.type() == json::value_t::boolean);
} }
} }