From 4503e5d9bc8b5c0c043931ecfacd748b058527b8 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 16 Nov 2026 07:04:51 +0100 Subject: [PATCH] Copy instead of move when converting an rvalue non-owning range Converting an rvalue array-like or object-like container to JSON moves its elements since the rvalue to_json overloads were added. The check only looked at the value category, so non-owning ranges (gsl::span, absl::Span, span backports, iterator ranges), which are usually passed by value, had the elements of the storage they refer to moved out: std::vector v = {...}; json j = make_span(v); // v's strings were left empty Move only from containers that own their elements, detected by deep constness: the elements of a const std::vector or std::map are const, while a const span still gives mutable elements. The rvalue overloads still take every rvalue container and copy the elements of the others, as in 3.12.0; this also keeps ranges that can only be iterated when not const working. Signed-off-by: Niels Lohmann --- docs/mkdocs/docs/api/basic_json/basic_json.md | 5 +- docs/mkdocs/docs/home/releases/unreleased.md | 4 +- .../nlohmann/detail/conversions/to_json.hpp | 60 ++++++---- single_include/nlohmann/json.hpp | 60 ++++++---- tests/src/unit-to_json-rvalue.cpp | 109 ++++++++++++++++++ 5 files changed, 194 insertions(+), 44 deletions(-) diff --git a/docs/mkdocs/docs/api/basic_json/basic_json.md b/docs/mkdocs/docs/api/basic_json/basic_json.md index c029f519c2..ee81ccac87 100644 --- a/docs/mkdocs/docs/api/basic_json/basic_json.md +++ b/docs/mkdocs/docs/api/basic_json/basic_json.md @@ -79,7 +79,10 @@ basic_json(basic_json&& other) noexcept; (e.g., `json j = std::move(vec);` or `json j(std::vector{...});`), the elements are moved into the JSON value instead of copied. Afterwards, the elements of the source container are in a valid but unspecified state (**moved-from**). Keys of associative containers are `const` and therefore still copied. - Lvalues and `const` rvalues are copied, and the source is left unchanged. + Lvalues and `const` rvalues are copied, and the source is left unchanged. Only containers that own their + elements are moved from, that is, containers whose elements are `const` when accessed through a `const` + container. Non-owning ranges such as spans or iterator ranges (e.g., `gsl::span`, `std::span`) are copied, so + converting an rvalue span leaves the referenced elements unchanged. - **numbers**: [`number_integer_t`](number_integer_t.md), [`number_unsigned_t`](number_unsigned_t.md), [`number_float_t`](number_float_t.md), and all convertible number types such as `int`, `size_t`, `int64_t`, `float` or `double` can be used. diff --git a/docs/mkdocs/docs/home/releases/unreleased.md b/docs/mkdocs/docs/home/releases/unreleased.md index 01b3342473..de156d3d0b 100644 --- a/docs/mkdocs/docs/home/releases/unreleased.md +++ b/docs/mkdocs/docs/home/releases/unreleased.md @@ -7,7 +7,9 @@ These changes are part of the `develop` branch, but not of a release yet. 2.3 ms). Two consequences: the source container is left in a valid but unspecified (moved-from) state, and if an exception is thrown during the conversion, the source may be partially moved-from, so the strong exception guarantee no longer holds for the source of an rvalue conversion (the JSON value itself is unaffected). Lvalues and `const` - objects are still copied. See [`basic_json`](../../api/basic_json/basic_json.md). + objects are still copied, and so are non-owning ranges such as spans (a range whose elements stay mutable through a + `const` range): converting an rvalue span leaves the referenced elements unchanged. + See [`basic_json`](../../api/basic_json/basic_json.md). - In C++20 (library support for `char8_t`, `__cpp_lib_char8_t`), a `#!cpp std::u8string` is a JSON **string**: `json j = std::u8string(u8"abc");` stores `"abc"`, and `#!cpp j.get()` reads it back. Version 3.12.0 stored a `#!cpp std::u8string` as an array of code units (`[97,98,99]`); on the 3.13.0 development branch it was diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index 3d95370d65..709440dcb8 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -48,30 +48,47 @@ namespace detail * leave j with the type of the new value, but the pointer to the destroyed old one. */ -// a move iterator over the elements of an rvalue container, unless they are -// const (e.g., std::set): libstdc++ before GCC 5 defines the reference of a -// std::move_iterator as value_type&&, which cannot bind to a const element -template -std::move_iterator make_move_iterator_if_mutable_impl(const Iterator& it, std::true_type /*mutable*/) -{ - return std::make_move_iterator(it); -} +// reference type of the elements of a const T +template +using const_range_reference_t = decltype(*std::declval&>()); -template -Iterator make_move_iterator_if_mutable_impl(const Iterator& it, std::false_type /*mutable*/) -{ - return it; -} +// a container that owns its elements propagates constness to them (deep +// constness): the elements of a const std::vector are const. A +// non-owning range (a span, an iterator range) has shallow constness: a const +// span still gives mutable access to the elements it refers to. Moving from an +// rvalue of such a range would empty the storage it refers to, so the elements +// of an rvalue range are only moved if it has deep constness. +template +using owns_its_elements = std::is_const>::type>; template using is_mutable_iterator = std::integral_constant < bool, !std::is_const())>::type>::value >; +// a move iterator over the elements of an rvalue container that owns them, +// unless they are const (e.g., std::set): libstdc++ before GCC 5 defines the +// reference of a std::move_iterator as value_type&&, which cannot bind to a +// const element template -auto make_move_iterator_if_mutable(const Iterator& it) --> decltype(make_move_iterator_if_mutable_impl(it, is_mutable_iterator {})) +std::move_iterator make_move_iterator_if_impl(const Iterator& it, std::true_type /*move*/) { - return make_move_iterator_if_mutable_impl(it, is_mutable_iterator {}); + return std::make_move_iterator(it); +} + +template +Iterator make_move_iterator_if_impl(const Iterator& it, std::false_type /*move*/) +{ + return it; +} + +template +using moves_elements = conjunction, is_mutable_iterator>; + +template +auto make_move_iterator_if_owned(const Iterator& it) +-> decltype(make_move_iterator_if_impl(it, moves_elements {})) +{ + return make_move_iterator_if_impl(it, moves_elements {}); } template struct external_constructor; @@ -193,14 +210,14 @@ struct external_constructor j.replace_value(value_t::array, value); } - /// an rvalue container: move its elements + /// an rvalue container: move its elements if it owns them template static void construct_moving(BasicJsonType& j, CompatibleArrayType& arr) { using std::begin; using std::end; - const typename BasicJsonType::json_value value(j.template create(make_move_iterator_if_mutable(begin(arr)), make_move_iterator_if_mutable(end(arr)))); + const typename BasicJsonType::json_value value(j.template create(make_move_iterator_if_owned(begin(arr)), make_move_iterator_if_owned(end(arr)))); j.replace_value(value_t::array, value); } @@ -254,14 +271,14 @@ struct external_constructor j.replace_value(value_t::object, value); } - /// an rvalue container: move its values + /// an rvalue container: move its values if it owns them template static void construct_moving(BasicJsonType& j, CompatibleObjectType& obj) { using std::begin; using std::end; - const typename BasicJsonType::json_value value(j.template create(make_move_iterator_if_mutable(begin(obj)), make_move_iterator_if_mutable(end(obj)))); + const typename BasicJsonType::json_value value(j.template create(make_move_iterator_if_owned(begin(obj)), make_move_iterator_if_owned(end(obj)))); j.replace_value(value_t::object, value); } @@ -456,7 +473,8 @@ using is_rvalue_array_to_json = conjunction, is_compatible_string_type, is_compatible_binary_type>>>; -// rvalue containers: their elements are moved instead of copied +// rvalue containers: their elements are moved instead of copied if the +// container owns them (see owns_its_elements) template::value, int> = 0> inline void to_json(BasicJsonType& j, CompatibleArrayType && arr) // NOLINT(cppcoreguidelines-missing-std-forward) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 473bfa894e..b849fdd5a0 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -7810,30 +7810,47 @@ namespace detail * leave j with the type of the new value, but the pointer to the destroyed old one. */ -// a move iterator over the elements of an rvalue container, unless they are -// const (e.g., std::set): libstdc++ before GCC 5 defines the reference of a -// std::move_iterator as value_type&&, which cannot bind to a const element -template -std::move_iterator make_move_iterator_if_mutable_impl(const Iterator& it, std::true_type /*mutable*/) -{ - return std::make_move_iterator(it); -} +// reference type of the elements of a const T +template +using const_range_reference_t = decltype(*std::declval&>()); -template -Iterator make_move_iterator_if_mutable_impl(const Iterator& it, std::false_type /*mutable*/) -{ - return it; -} +// a container that owns its elements propagates constness to them (deep +// constness): the elements of a const std::vector are const. A +// non-owning range (a span, an iterator range) has shallow constness: a const +// span still gives mutable access to the elements it refers to. Moving from an +// rvalue of such a range would empty the storage it refers to, so the elements +// of an rvalue range are only moved if it has deep constness. +template +using owns_its_elements = std::is_const>::type>; template using is_mutable_iterator = std::integral_constant < bool, !std::is_const())>::type>::value >; +// a move iterator over the elements of an rvalue container that owns them, +// unless they are const (e.g., std::set): libstdc++ before GCC 5 defines the +// reference of a std::move_iterator as value_type&&, which cannot bind to a +// const element template -auto make_move_iterator_if_mutable(const Iterator& it) --> decltype(make_move_iterator_if_mutable_impl(it, is_mutable_iterator {})) +std::move_iterator make_move_iterator_if_impl(const Iterator& it, std::true_type /*move*/) { - return make_move_iterator_if_mutable_impl(it, is_mutable_iterator {}); + return std::make_move_iterator(it); +} + +template +Iterator make_move_iterator_if_impl(const Iterator& it, std::false_type /*move*/) +{ + return it; +} + +template +using moves_elements = conjunction, is_mutable_iterator>; + +template +auto make_move_iterator_if_owned(const Iterator& it) +-> decltype(make_move_iterator_if_impl(it, moves_elements {})) +{ + return make_move_iterator_if_impl(it, moves_elements {}); } template struct external_constructor; @@ -7955,14 +7972,14 @@ struct external_constructor j.replace_value(value_t::array, value); } - /// an rvalue container: move its elements + /// an rvalue container: move its elements if it owns them template static void construct_moving(BasicJsonType& j, CompatibleArrayType& arr) { using std::begin; using std::end; - const typename BasicJsonType::json_value value(j.template create(make_move_iterator_if_mutable(begin(arr)), make_move_iterator_if_mutable(end(arr)))); + const typename BasicJsonType::json_value value(j.template create(make_move_iterator_if_owned(begin(arr)), make_move_iterator_if_owned(end(arr)))); j.replace_value(value_t::array, value); } @@ -8016,14 +8033,14 @@ struct external_constructor j.replace_value(value_t::object, value); } - /// an rvalue container: move its values + /// an rvalue container: move its values if it owns them template static void construct_moving(BasicJsonType& j, CompatibleObjectType& obj) { using std::begin; using std::end; - const typename BasicJsonType::json_value value(j.template create(make_move_iterator_if_mutable(begin(obj)), make_move_iterator_if_mutable(end(obj)))); + const typename BasicJsonType::json_value value(j.template create(make_move_iterator_if_owned(begin(obj)), make_move_iterator_if_owned(end(obj)))); j.replace_value(value_t::object, value); } @@ -8218,7 +8235,8 @@ using is_rvalue_array_to_json = conjunction, is_compatible_string_type, is_compatible_binary_type>>>; -// rvalue containers: their elements are moved instead of copied +// rvalue containers: their elements are moved instead of copied if the +// container owns them (see owns_its_elements) template::value, int> = 0> inline void to_json(BasicJsonType& j, CompatibleArrayType && arr) // NOLINT(cppcoreguidelines-missing-std-forward) diff --git a/tests/src/unit-to_json-rvalue.cpp b/tests/src/unit-to_json-rvalue.cpp index 4aabda5a16..c078e01032 100644 --- a/tests/src/unit-to_json-rvalue.cpp +++ b/tests/src/unit-to_json-rvalue.cpp @@ -67,6 +67,67 @@ void to_json(json& j, tracked&& t) t.moved_from = true; } +// non-owning range over contiguous storage (like gsl::span or a pre-C++20 span +// backport): its constness is shallow, so a const span gives mutable elements +template +struct span_like +{ + using value_type = T; + using iterator = T*; + using const_iterator = T*; + + T* first; + T* last; + + T* begin() const + { + return first; + } + T* end() const + { + return last; + } +}; + +// non-owning view of key/value pairs with key_type and mapped_type, so it is +// converted like an object +template +struct map_span_like +{ + using key_type = K; + using mapped_type = V; + using value_type = std::pair; + using iterator = value_type*; + using const_iterator = value_type*; + + value_type* first; + value_type* last; + + value_type* begin() const + { + return first; + } + value_type* end() const + { + return last; + } +}; + +// owning container that can only be iterated when it is not const +struct mutable_only_range +{ + std::vector values; + + std::vector::iterator begin() + { + return values.begin(); + } + std::vector::iterator end() + { + return values.end(); + } +}; + // user type with its own rvalue-aware serializer struct udt_container { @@ -144,6 +205,34 @@ TEST_CASE("to_json from rvalue containers") CHECK(tracked_moves == 2); } + SECTION("tracked elements: standard containers move") + { + tracked_copies = 0; + tracked_moves = 0; + + std::list l; + l.emplace_back("l"); + CHECK(json(std::move(l)) == json::array({"l"})); + + std::deque d; + d.emplace_back("d"); + CHECK(json(std::move(d)) == json::array({"d"})); + + std::forward_list fl; + fl.emplace_front("f"); + CHECK(json(std::move(fl)) == json::array({"f"})); + + std::array a{{tracked("a")}}; + CHECK(json(std::move(a)) == json::array({"a"})); + + std::unordered_map um; + um.emplace("k", tracked("u")); + CHECK(json(std::move(um)) == json({{"k", "u"}})); + + CHECK(tracked_moves == 5); + CHECK(tracked_copies == 0); + } + SECTION("const rvalue is copied") { tracked_copies = 0; @@ -221,6 +310,26 @@ TEST_CASE("to_json from rvalue containers") CHECK(json(std::move(obj)) == json({{"a", 1}})); } + SECTION("non-owning ranges are copied") + { + std::vector v{long_a, long_b}; + const json j = span_like {v.data(), v.data() + v.size()}; + CHECK(j == json::array({long_a, long_b})); + CHECK(v[0] == long_a); + CHECK(v[1] == long_b); + + std::vector> m{{"a", long_a}}; + const json jm = map_span_like {m.data(), m.data() + m.size()}; + CHECK(jm == json({{"a", long_a}})); + CHECK(m[0].second == long_a); + + // without a const begin(), it cannot be told whether the range owns + // its elements, so they are copied + mutable_only_range r{{long_a}}; + CHECK(json(std::move(r)) == json::array({long_a})); + CHECK(r.values[0] == long_a); // NOLINT(bugprone-use-after-move,hicpp-invalid-access-moved) + } + SECTION("user type with its own to_json overloads") { udt_container c{{1, 2}};