You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
thanks for this awesome library. I've got a json file with an array of strings and I am wondering if it would be possible to get a reference of std::vectorstd::string?
GCC complains about error: no matching function for call to ‘nlohmann::basic_json<>::get_impl_ptr(const std::vector<std::__cxx11::basic_string<char> >*) const’
Would it be possible to fix this?
The text was updated successfully, but these errors were encountered:
Based on get_ref() documentation, it supports only the basic json types (e.g. array_t). I doubt a zero-cost cast is be possible from the underlying json type to std::vector<std::string>. This is in contrast to get() method which can create a fresh copy of given type if needed.
You can still get a reference to the array though. Here is an demo: https://godbolt.org/z/9o5Yrv4zE
Note that I have modified the array just for demonstration and I'm not sure if this is allowed.
You cannot convert references - the values are internally stored as array_t<json> (with std::vector being the default type for array_t), so @nickaein is right that get_ref<json::array_t&>() is as far as you can go.
Thanks for your help. I think using a reference to array_t isn't an option for me. Im my application I have one module 'A' which parses some json config and some other module 'B' which uses the parsed values. I would like module 'B' to stay independent from the json library.
The array in the config won't be big and so I can will live with the copy of std::vector<std::string> instead.
Hi,
thanks for this awesome library. I've got a json file with an array of strings and I am wondering if it would be possible to get a reference of std::vectorstd::string?
This code is working fine:
nlohmann::json fileContent = json::parse(...); std::vector<std::string> array = fileContent["array"].get<std::vector<std::string>>();
But trying to get a reference doesn't:
GCC complains about
error: no matching function for call to ‘nlohmann::basic_json<>::get_impl_ptr(const std::vector<std::__cxx11::basic_string<char> >*) const’
Would it be possible to fix this?
The text was updated successfully, but these errors were encountered: