diff --git a/docs/mkdocs/docs/api/basic_json/get_to.md b/docs/mkdocs/docs/api/basic_json/get_to.md index 8334ba5cf..c50c077b0 100644 --- a/docs/mkdocs/docs/api/basic_json/get_to.md +++ b/docs/mkdocs/docs/api/basic_json/get_to.md @@ -21,6 +21,10 @@ This overload is chosen if: - `ValueType` is not `basic_json`, - `json_serializer` has a `from_json()` method of the form `void from_json(const basic_json&, ValueType&)` +`v` must not be `const`. Passing a `const` object is a compile-time error. For types such as arithmetic types, enums, +and C arrays, the error is a `static_assert` that names the problem. For other types, the overload is not viable, and +the compiler reports that no matching `get_to` was found. + ## Template parameters `ValueType` @@ -67,3 +71,4 @@ Depends on the `json_serializer::from_json()` implementation. ## Version history - Since version 3.3.0. +- Added a `static_assert` with a clear message for `const` arguments in version 3.13.0. diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 0123bad29..1090ed105 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -2553,6 +2553,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ValueType & get_to(ValueType& v) const noexcept(noexcept( JSONSerializer::from_json(std::declval(), v))) { + static_assert(!std::is_const::value, "get_to() cannot deserialize into a const value"); JSONSerializer::from_json(*this, v); return v; } @@ -2578,6 +2579,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec noexcept(noexcept(JSONSerializer::from_json( std::declval(), v))) { + static_assert(!std::is_const::value, "get_to() cannot deserialize into a const value"); JSONSerializer::from_json(*this, v); return v; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 4567a2b32..8c3a1319b 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -27198,6 +27198,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ValueType & get_to(ValueType& v) const noexcept(noexcept( JSONSerializer::from_json(std::declval(), v))) { + static_assert(!std::is_const::value, "get_to() cannot deserialize into a const value"); JSONSerializer::from_json(*this, v); return v; } @@ -27223,6 +27224,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec noexcept(noexcept(JSONSerializer::from_json( std::declval(), v))) { + static_assert(!std::is_const::value, "get_to() cannot deserialize into a const value"); JSONSerializer::from_json(*this, v); return v; }