Skip to content
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

How to get reference of std::vector<T> #2735

Closed
pschmale-grimme opened this issue Apr 22, 2021 · 3 comments
Closed

How to get reference of std::vector<T> #2735

pschmale-grimme opened this issue Apr 22, 2021 · 3 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@pschmale-grimme
Copy link

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?

{ "array": ["val1", "val2"]}

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:

const std::vector<std::string>& array = fileContent["array"].get_ref<const std::vector<std::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?

@nickaein
Copy link
Contributor

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.

@nlohmann
Copy link
Owner

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.

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Apr 23, 2021
@pschmale-grimme
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants