Skip to content

Commit f36fd05

Browse files
committed
remove const version, add a test case for scrambled number representations.
Signed-off-by: Evelyn LePain <ava.lepain@gmail.com>
1 parent 3271b8a commit f36fd05

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

include/nlohmann/detail/conversions/from_json.hpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -452,24 +452,16 @@ detail::uncvref_t<Type> from_json_tuple_get_impl(BasicJsonType&& j, detail::iden
452452
return std::forward<BasicJsonType>(j).template get<detail::uncvref_t<Type>>();
453453
}
454454

455-
template<typename BasicJsonType, typename Type,
456-
typename ConstType = typename std::remove_reference<Type>::type const&,
457-
detail::enable_if_t<detail::is_compatible_reference_type<BasicJsonType, ConstType>::value, int> = 0>
458-
ConstType from_json_tuple_get_impl(BasicJsonType && j, detail::identity_tag<Type> /*unused*/, detail::priority_tag<1> /*unused*/)
459-
{
460-
return std::forward<BasicJsonType>(j).template get_ref<ConstType>();
461-
}
462-
463455
template<typename BasicJsonType, typename Type,
464456
detail::enable_if_t<detail::is_compatible_reference_type<BasicJsonType, Type>::value, int> = 0>
465-
Type from_json_tuple_get_impl(BasicJsonType && j, detail::identity_tag<Type> /*unused*/, detail::priority_tag<2> /*unused*/)
457+
Type from_json_tuple_get_impl(BasicJsonType && j, detail::identity_tag<Type> /*unused*/, detail::priority_tag<1> /*unused*/)
466458
{
467459
return std::forward<BasicJsonType>(j).template get_ref<Type>();
468460
}
469461

470462
template<typename BasicJsonType, typename Type,
471463
detail::enable_if_t<std::is_arithmetic<uncvref_t<Type>>::value, int> = 0>
472-
detail::uncvref_t<Type> from_json_tuple_get_impl(BasicJsonType && j, detail::identity_tag<Type> /*unused*/, detail::priority_tag<3> /*unused*/)
464+
detail::uncvref_t<Type> from_json_tuple_get_impl(BasicJsonType && j, detail::identity_tag<Type> /*unused*/, detail::priority_tag<2> /*unused*/)
473465
{
474466
return std::forward<BasicJsonType>(j).template get<detail::uncvref_t<Type>>();
475467
}
@@ -507,13 +499,13 @@ std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tu
507499
{
508500
static_assert(cxpr_and<cxpr_or<cxpr_not<std::is_reference<Args>>, is_compatible_reference_type<BasicJsonType, Args>>...>::value,
509501
"Can not return a tuple containing references to types not contained in a Json, try Json::get_to()");
510-
return from_json_tuple_impl_base<2, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
502+
return from_json_tuple_impl_base<1, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
511503
}
512504

513505
template<typename BasicJsonType, typename... Args>
514506
inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
515507
{
516-
t = from_json_tuple_impl_base<3, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
508+
t = from_json_tuple_impl_base<2, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
517509
}
518510

519511
template<typename BasicJsonType, typename TupleRelated>

single_include/nlohmann/json.hpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5355,24 +5355,16 @@ detail::uncvref_t<Type> from_json_tuple_get_impl(BasicJsonType&& j, detail::iden
53555355
return std::forward<BasicJsonType>(j).template get<detail::uncvref_t<Type>>();
53565356
}
53575357

5358-
template<typename BasicJsonType, typename Type,
5359-
typename ConstType = typename std::remove_reference<Type>::type const&,
5360-
detail::enable_if_t<detail::is_compatible_reference_type<BasicJsonType, ConstType>::value, int> = 0>
5361-
ConstType from_json_tuple_get_impl(BasicJsonType && j, detail::identity_tag<Type> /*unused*/, detail::priority_tag<1> /*unused*/)
5362-
{
5363-
return std::forward<BasicJsonType>(j).template get_ref<ConstType>();
5364-
}
5365-
53665358
template<typename BasicJsonType, typename Type,
53675359
detail::enable_if_t<detail::is_compatible_reference_type<BasicJsonType, Type>::value, int> = 0>
5368-
Type from_json_tuple_get_impl(BasicJsonType && j, detail::identity_tag<Type> /*unused*/, detail::priority_tag<2> /*unused*/)
5360+
Type from_json_tuple_get_impl(BasicJsonType && j, detail::identity_tag<Type> /*unused*/, detail::priority_tag<1> /*unused*/)
53695361
{
53705362
return std::forward<BasicJsonType>(j).template get_ref<Type>();
53715363
}
53725364

53735365
template<typename BasicJsonType, typename Type,
53745366
detail::enable_if_t<std::is_arithmetic<uncvref_t<Type>>::value, int> = 0>
5375-
detail::uncvref_t<Type> from_json_tuple_get_impl(BasicJsonType && j, detail::identity_tag<Type> /*unused*/, detail::priority_tag<3> /*unused*/)
5367+
detail::uncvref_t<Type> from_json_tuple_get_impl(BasicJsonType && j, detail::identity_tag<Type> /*unused*/, detail::priority_tag<2> /*unused*/)
53765368
{
53775369
return std::forward<BasicJsonType>(j).template get<detail::uncvref_t<Type>>();
53785370
}
@@ -5410,13 +5402,13 @@ std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tu
54105402
{
54115403
static_assert(cxpr_and<cxpr_or<cxpr_not<std::is_reference<Args>>, is_compatible_reference_type<BasicJsonType, Args>>...>::value,
54125404
"Can not return a tuple containing references to types not contained in a Json, try Json::get_to()");
5413-
return from_json_tuple_impl_base<2, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
5405+
return from_json_tuple_impl_base<1, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
54145406
}
54155407

54165408
template<typename BasicJsonType, typename... Args>
54175409
inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
54185410
{
5419-
t = from_json_tuple_impl_base<3, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
5411+
t = from_json_tuple_impl_base<2, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
54205412
}
54215413

54225414
template<typename BasicJsonType, typename TupleRelated>

tests/src/unit-constructor1.cpp

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,19 @@ TEST_CASE("constructors")
283283

284284
SECTION("std::tuple tie")
285285
{
286-
auto a = 1.0;
287-
auto b = "string";
288-
auto c = 42;
289-
auto d = std::vector<int> {0, 2};
290-
size_t e = 1234;
286+
const auto a = 1.0;
287+
const auto* const b = "string";
288+
const auto c = 42;
289+
const auto d = std::vector<int> {0, 2};
290+
const size_t e = 1234;
291291
auto t = std::tie(a, b, c, d, e);
292292
json const j(t);
293293

294-
double a_out;
294+
double a_out = 0;
295295
std::string b_out;
296-
int c_out;
296+
int c_out = 0;
297297
std::vector<int> d_out;
298-
int64_t e_out;
298+
int64_t e_out = 0;
299299
auto t_out = std::tie(a_out, b_out, c_out, d_out, e_out);
300300
j.get_to(t_out);
301301
CHECK(a_out == a);
@@ -308,7 +308,7 @@ TEST_CASE("constructors")
308308
SECTION("std::tuple of references to elements")
309309
{
310310
const auto a = 1.0;
311-
const auto b = "string";
311+
const auto* const b = "string";
312312
const auto c = 42;
313313
const size_t d = 1234;
314314
const auto t = std::tie(a, b, c, d);
@@ -328,6 +328,32 @@ TEST_CASE("constructors")
328328
CHECK(std::get<3>(t_out) == d);
329329
}
330330

331+
SECTION("std::tuple mixed arithmetic types")
332+
{
333+
using j_float_t = json::number_float_t;
334+
using j_int_t = json::number_integer_t;
335+
using j_uint_t = json::number_unsigned_t;
336+
const j_float_t a = 1.0;
337+
const j_int_t b = 1234;
338+
const j_uint_t c = 42;
339+
json const j(std::tie(a, b, c, c));
340+
341+
auto t1 = j.get<std::tuple<j_int_t, j_uint_t, j_float_t, const j_uint_t&>>();
342+
j_uint_t a2 = 0;
343+
j_float_t b2 = 0;
344+
j_int_t c2 = 0;
345+
auto t2 = std::tie(a2, b2, c2);
346+
j.get_to(t2);
347+
348+
CHECK(std::get<0>(t1) == a);
349+
CHECK(std::get<1>(t1) == b);
350+
CHECK(std::get<2>(t1) == c);
351+
// t1[3] exists only to force usage of the no-default-constructor version
352+
CHECK(a2 == a);
353+
CHECK(b2 == b);
354+
CHECK(c2 == c);
355+
}
356+
331357
SECTION("std::pair/tuple/array failures")
332358
{
333359
json const j{1};

0 commit comments

Comments
 (0)