From d27adfd02149ff3e86ad1def158bbde34978ef21 Mon Sep 17 00:00:00 2001 From: Fredrik Sandhei Date: Tue, 19 Mar 2024 22:16:04 +0100 Subject: [PATCH] Fix failing test --- include/nlohmann/detail/conversions/from_json.hpp | 2 +- single_include/nlohmann/json.hpp | 3 ++- tests/src/unit-conversions.cpp | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index acf6a80f46..a51cb53db2 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -53,7 +53,7 @@ void from_json(const BasicJsonType& j, std::optional& opt) } else { - opt = j.template get(); + opt.emplace(j.template get()); } } #endif diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 5d0ba6efb6..6940d479b8 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2407,6 +2407,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wfloat-equal" +#endif #ifdef __has_include #if __has_include() #include @@ -4624,7 +4625,7 @@ void from_json(const BasicJsonType& j, std::optional& opt) } else { - opt = j.template get(); + opt.emplace(j.template get()); } } #endif diff --git a/tests/src/unit-conversions.cpp b/tests/src/unit-conversions.cpp index 5e838e6e1a..939909ec98 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -1578,7 +1578,7 @@ TEST_CASE("std::optional") std::optional opt_null; CHECK(json(opt_null) == j_null); - CHECK(std::optional(j_null) == std::nullopt); + CHECK(j_null.get>() == std::nullopt); } SECTION("string")