Skip to content

Memory allocations using range-based for loops #214

@habemus-papadum

Description

@habemus-papadum

In the Readme, the following example is provided:

for (auto element : j) {
  std::cout << element << '\n';
}

Does this end up creating a copy of each array element during each iteration?

Are

for (const auto &element : j) {
  std::cout << element << '\n';
}

and

for (auto &element : j) {
  element["a"]=...;
}

better alternatives or is the version in the readme the optimal usage for all scenarios?

(In some sense this is just a basic c++ question. I don't really know how ranged for loop work in c++, or how to actually check what is happening in each of the scenarios, but in any case I am specifically curious about it works for basic_json's implementation)

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions