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

Get value without explicit typecasting #1395

Closed
mprat opened this issue Dec 14, 2018 · 2 comments
Closed

Get value without explicit typecasting #1395

mprat opened this issue Dec 14, 2018 · 2 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@mprat
Copy link

mprat commented Dec 14, 2018

I was wondering if there was a way to get the underlying value of an object without explicit typecasting. Here is what I mean:

json j = R"({"int_type": 3})"_json;

auto i_what_is_this = j["int_type"];

// to get an int, I need to explicitly typecast
auto i_int = j["int_type"].get<int>();

JSON natively supports different types, so is there a way to use that underling type to get auto i = j["int_type"]; to be of type int without explicitly typecasting?

@gregmarr
Copy link
Contributor

No, the type in the object is a runtime type, and auto requires compile-time types. In this case, i_what_is_this is json.

@nlohmann
Copy link
Owner

@gregmarr is right. The only stylistic choice you have is to replace

auto i_int = j["int_type"].get<int>();

by

int i_int = j["int_type"];

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Dec 16, 2018
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