From c4469c9f82ffb1ed079af12015ddd0515deefcb7 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 27 Sep 2026 16:49:33 +0200 Subject: [PATCH] Add regression test for converting json to std::variant (#5066) With 3.10.5, get>() was well-formed through the string from_json overload, so the implicit conversion operator was a candidate when converting json to std::variant, and MSVC picked it over the variant's converting constructor. The tightened constraints from #3427 and #3604 (3.11.0) removed that path; this test guards against regressions. Signed-off-by: Niels Lohmann --- tests/src/unit-regression2.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/src/unit-regression2.cpp b/tests/src/unit-regression2.cpp index b128b7a73..bb2c377ac 100644 --- a/tests/src/unit-regression2.cpp +++ b/tests/src/unit-regression2.cpp @@ -765,6 +765,20 @@ TEST_CASE("regression tests 2") CHECK(j == k); } +#ifdef JSON_HAS_CPP_17 + SECTION("issue #5066 - MSVC converts json to std::variant via the conversion operator") + { + // std::variant must not be retrievable via get<>(), because otherwise the + // implicit conversion operator becomes a candidate that MSVC picks over the variant's + // converting constructor, routing a number through the string from_json overload + static_assert(!nlohmann::detail::is_detected>::value, + "std::variant must not be retrievable via get<>()"); + + std::vector> v; + v.push_back(json(1)); + CHECK(std::get<0>(v[0]) == 1); + } +#endif } TEST_CASE("regression test - parser callback must not lose a duplicate key's prior value")