-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More use of move semantics in deserialization #786
Comments
Hello, do you have a small example that fails to compile? If I understand you correctly, the bug is in the library-provided conversions? |
I just want to avoid copies when filling the e.g. an |
The following code works fine, there is no copy involved: #include <iostream>
#include <json.hpp>
struct test {
test() = default;
test(test const &) = delete;
test(test &&) { std::cout << "move" << std::endl; };
test &operator=(test const &) = delete;
test &operator=(test &&) = default;
~test() = default;
};
namespace nlohmann {
template <> struct adl_serializer<::test> {
static ::test from_json(json const &) { return {}; }
};
}
int main(int argc, char const *argv[]) {
auto j = nlohmann::json{1, 2, 3};
auto v = j.get<std::vector<test>>();
v.emplace_back(nlohmann::json(1));
} I think I misunderstand your issue, feel free to post some code :) |
Hum, it seems that you have well understood that issue, and your snippet works well. Issue must be on my side (I am tracking assignments and seeing a few). Closing, thanks for your reply. |
Short version
For certain cases, where copying things is an issue (as the one described below), I would like to favor examples similar to
move_only_type
over the creation of afrom_json
function, but specializations ofadl_serializer
does not seem to work with containers of types for which the specialization exist.Ideally, I would like to have a "return by value" version of
from_json
. The STL container of those types would be built by emplacing the values returned byfrom_json
into the container.Long version / context
We currently use your library (vendoring the json header for now) in xeus, a native C++ implementation of the Jupyter protocol.
In this work, some of the classes that we deserialize from json have a value semantics and implement the RAII pattern. Basically, constructor and destructors amount to acquiring and freeing the resources. Any non-move copy acquires a new version of the resource (with a different id). Move constructor and assignment transfers the resource to the moved-to object.
This is a context in which being able to move things to a container would make a lot of sense...
The text was updated successfully, but these errors were encountered: