Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf1e3407

Browse files
Fix 'const' qualifier on bool& has no effect (nlohmann#3678)
* Fix 'const' qualifier on bool& has no effectThanks,@georgthegreat, for pointing out this issue.* Extend std::vector<bool> unit test
1 parentbfbe774 commitf1e3407

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

‎include/nlohmann/detail/conversions/to_json.hpp‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,15 @@ inline void to_json(BasicJsonType& j, T b) noexcept
267267
external_constructor<value_t::boolean>::construct(j, b);
268268
}
269269

270-
template<typename BasicJsonType,
271-
enable_if_t<std::is_convertible<const std::vector<bool>::reference&,typename BasicJsonType::boolean_t>::value,int> =0>
272-
inlinevoidto_json(BasicJsonType& j,const std::vector<bool>::reference& b)noexcept
270+
template<typename BasicJsonType,typename BoolRef,
271+
enable_if_t <
272+
((std::is_same<std::vector<bool>::reference, BoolRef>::value
273+
&& !std::is_same <std::vector<bool>::reference,typename BasicJsonType::boolean_t&>::value)
274+
|| (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
275+
&& !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
276+
typename BasicJsonType::boolean_t >::value))
277+
&& std::is_convertible<const BoolRef&,typename BasicJsonType::boolean_t>::value,int > =0 >
278+
inlinevoidto_json(BasicJsonType& j,const BoolRef& b)noexcept
273279
{
274280
external_constructor<value_t::boolean>::construct(j,static_cast<typename BasicJsonType::boolean_t>(b));
275281
}

‎single_include/nlohmann/json.hpp‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5500,9 +5500,15 @@ inline void to_json(BasicJsonType& j, T b) noexcept
55005500
external_constructor<value_t::boolean>::construct(j, b);
55015501
}
55025502

5503-
template<typename BasicJsonType,
5504-
enable_if_t<std::is_convertible<const std::vector<bool>::reference&, typename BasicJsonType::boolean_t>::value, int> = 0>
5505-
inline void to_json(BasicJsonType& j, const std::vector<bool>::reference& b) noexcept
5503+
template < typename BasicJsonType, typename BoolRef,
5504+
enable_if_t <
5505+
((std::is_same<std::vector<bool>::reference, BoolRef>::value
5506+
&& !std::is_same <std::vector<bool>::reference, typename BasicJsonType::boolean_t&>::value)
5507+
|| (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
5508+
&& !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
5509+
typename BasicJsonType::boolean_t >::value))
5510+
&& std::is_convertible<const BoolRef&, typename BasicJsonType::boolean_t>::value, int > = 0 >
5511+
inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept
55065512
{
55075513
external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
55085514
}

‎tests/src/unit-constructor1.cpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,19 @@ TEST_CASE("constructors")
454454
CHECK(j.type() == json::value_t::boolean);
455455
}
456456

457-
SECTION("from std::vector<bool>::refrence")
457+
SECTION("from std::vector<bool>::reference")
458458
{
459459
std::vector<bool> v{true};
460460
jsonj(v[0]);
461+
CHECK(std::is_same<decltype(v[0]), std::vector<bool>::reference>::value);
462+
CHECK(j.type() == json::value_t::boolean);
463+
}
464+
465+
SECTION("from std::vector<bool>::const_reference")
466+
{
467+
const std::vector<bool> v{true};
468+
jsonj(v[0]);
469+
CHECK(std::is_same<decltype(v[0]), std::vector<bool>::const_reference>::value);
461470
CHECK(j.type() == json::value_t::boolean);
462471
}
463472
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp