Compare commits

...

2 Commits

Author SHA1 Message Date
Niels Lohmann 97dc6bed50 Guard optional assignment test behind JSON_USE_IMPLICIT_CONVERSIONS
opt_assign = j_null relies on basic_json's implicit conversion
operator to satisfy std::optional's assignment operator template
(which requires is_assignable, not just is_constructible). With
implicit conversions disabled the operator is explicit, so the
assignment doesn't compile at all rather than throwing at runtime.
Fixes the ci_test_noimplicitconversions failure on #5269.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-07-11 23:42:29 +02:00
Niels Lohmann 0e535ba3bc Extend std::optional null regression coverage (assignment, get_to)
Adds two cases to the existing null-handling regression test for
std::optional<T>: assignment (opt = json_null) throws for the same
reason as direct construction, and get_to() correctly yields
std::nullopt. Prompted by a review comment on #5246 asking for a
fuller regression matrix; construction and get<optional<T>>() were
already covered by #5247.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-07-11 18:49:11 +02:00
+15
View File
@@ -1782,6 +1782,21 @@ TEST_CASE("std::optional")
"[json.exception.type_error.302] type must be string, but is null", json::type_error&); "[json.exception.type_error.302] type must be string, but is null", json::type_error&);
CHECK_THROWS_WITH_AS(std::optional<int>(j_null), CHECK_THROWS_WITH_AS(std::optional<int>(j_null),
"[json.exception.type_error.302] type must be number, but is null", json::type_error&); "[json.exception.type_error.302] type must be number, but is null", json::type_error&);
// Assignment goes through the same overload resolution as direct
// construction, so it throws for the same reason. This relies on
// basic_json's implicit conversion operator, so it only applies
// when JSON_USE_IMPLICIT_CONVERSIONS is enabled (the default).
#if JSON_USE_IMPLICIT_CONVERSIONS
std::optional<std::string> opt_assign;
CHECK_THROWS_WITH_AS(opt_assign = j_null,
"[json.exception.type_error.302] type must be string, but is null", json::type_error&);
#endif
// get_to() is the correct way to obtain std::nullopt from a JSON null.
std::optional<std::string> opt_get_to = "placeholder";
j_null.get_to(opt_get_to);
CHECK(opt_get_to == std::nullopt);
} }
SECTION("string") SECTION("string")