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

Range-based iteration over a non-array object #998

Closed
dvhwgumby opened this issue Mar 6, 2018 · 3 comments
Closed

Range-based iteration over a non-array object #998

dvhwgumby opened this issue Mar 6, 2018 · 3 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@dvhwgumby
Copy link

It is possible or planned to be able to use a range-based for loop to iterate over a non-array object?

That is, you can do:

for (auto& element : j) 
  std::cout << element << std::endl;

for an array [ "a", "b" ] but not for {"foo": "bar", "quux":"baaz"} (well, range-based iteration works if you don't care about the key)

The json package compiles and works just fine under C++17, so I would like to be able to do:

for (auto& [key, value] : j)
  std::cout << key << " : " << value << std::endl;

I am sure there are more important feature requests (e.g. SAX read & write) but I thought I would submit this one anyway. This package is great! Thanks, d

@nlohmann
Copy link
Owner

nlohmann commented Mar 6, 2018

You can do this with the items() function.

For structured bindings, see #901.

@dvhwgumby
Copy link
Author

Thanks

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Mar 6, 2018
@nlohmann
Copy link
Owner

FYI: Structured binding support has recently been merged (#1391) and will be part of the next release.

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

2 participants