From 97dc6bed50f58143f68ba1e8b91f5249fb1717ac Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 11 Jul 2026 23:42:29 +0200 Subject: [PATCH] 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 --- tests/src/unit-conversions.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/src/unit-conversions.cpp b/tests/src/unit-conversions.cpp index d290659d5..1937affbb 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -1784,10 +1784,14 @@ TEST_CASE("std::optional") "[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. + // 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 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 opt_get_to = "placeholder";