From f1e34070d2966f8794b79fe85fb3c8cae868c48a Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sun, 7 Aug 2022 13:50:08 +0200 Subject: [PATCH] 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 unit test --- include/nlohmann/detail/conversions/to_json.hpp | 12 +++++++++--- single_include/nlohmann/json.hpp | 12 +++++++++--- tests/src/unit-constructor1.cpp | 11 ++++++++++- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index ba24c118d..f10a7393e 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -267,9 +267,15 @@ inline void to_json(BasicJsonType& j, T b) noexcept external_constructor::construct(j, b); } -template::reference&, typename BasicJsonType::boolean_t>::value, int> = 0> -inline void to_json(BasicJsonType& j, const std::vector::reference& b) noexcept +template < typename BasicJsonType, typename BoolRef, + enable_if_t < + ((std::is_same::reference, BoolRef>::value + && !std::is_same ::reference, typename BasicJsonType::boolean_t&>::value) + || (std::is_same::const_reference, BoolRef>::value + && !std::is_same ::const_reference>, + typename BasicJsonType::boolean_t >::value)) + && std::is_convertible::value, int > = 0 > +inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept { external_constructor::construct(j, static_cast(b)); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index beee0136c..6668a173b 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -5500,9 +5500,15 @@ inline void to_json(BasicJsonType& j, T b) noexcept external_constructor::construct(j, b); } -template::reference&, typename BasicJsonType::boolean_t>::value, int> = 0> -inline void to_json(BasicJsonType& j, const std::vector::reference& b) noexcept +template < typename BasicJsonType, typename BoolRef, + enable_if_t < + ((std::is_same::reference, BoolRef>::value + && !std::is_same ::reference, typename BasicJsonType::boolean_t&>::value) + || (std::is_same::const_reference, BoolRef>::value + && !std::is_same ::const_reference>, + typename BasicJsonType::boolean_t >::value)) + && std::is_convertible::value, int > = 0 > +inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept { external_constructor::construct(j, static_cast(b)); } diff --git a/tests/src/unit-constructor1.cpp b/tests/src/unit-constructor1.cpp index f294e5cd6..9e62a09f8 100644 --- a/tests/src/unit-constructor1.cpp +++ b/tests/src/unit-constructor1.cpp @@ -454,10 +454,19 @@ TEST_CASE("constructors") CHECK(j.type() == json::value_t::boolean); } - SECTION("from std::vector::refrence") + SECTION("from std::vector::reference") { std::vector v{true}; json j(v[0]); + CHECK(std::is_same::reference>::value); + CHECK(j.type() == json::value_t::boolean); + } + + SECTION("from std::vector::const_reference") + { + const std::vector v{true}; + json j(v[0]); + CHECK(std::is_same::const_reference>::value); CHECK(j.type() == json::value_t::boolean); } }