{"action":"created","issue":{"url":"https://api.github.com/repos/nlohmann/json/issues/214","repository_url":"https://api.github.com/repos/nlohmann/json","labels_url":"https://api.github.com/repos/nlohmann/json/issues/214/labels{/name}","comments_url":"https://api.github.com/repos/nlohmann/json/issues/214/comments","events_url":"https://api.github.com/repos/nlohmann/json/issues/214/events","html_url":"https://github.com/nlohmann/json/issues/214","id":136950988,"number":214,"title":"Memory allocations using range-based for loops","user":{"login":"lilinjn","id":1161604,"avatar_url":"https://avatars.githubusercontent.com/u/1161604?v=3","gravatar_id":"","url":"https://api.github.com/users/lilinjn","html_url":"https://github.com/lilinjn","followers_url":"https://api.github.com/users/lilinjn/followers","following_url":"https://api.github.com/users/lilinjn/following{/other_user}","gists_url":"https://api.github.com/users/lilinjn/gists{/gist_id}","starred_url":"https://api.github.com/users/lilinjn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lilinjn/subscriptions","organizations_url":"https://api.github.com/users/lilinjn/orgs","repos_url":"https://api.github.com/users/lilinjn/repos","events_url":"https://api.github.com/users/lilinjn/events{/privacy}","received_events_url":"https://api.github.com/users/lilinjn/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/nlohmann/json/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-02-27T18:16:23Z","updated_at":"2016-02-28T23:58:15Z","closed_at":null,"body":"In the Readme, the following example is provided:\r\n````\r\nfor (auto element : j) {\r\n  std::cout << element << '\\n';\r\n}\r\n````\r\n\r\nDoes this end up creating a copy of each array element during each iteration?\r\n\r\nAre \r\n````\r\nfor (const auto &element : j) {\r\n  std::cout << element << '\\n';\r\n}\r\n````\r\nand \r\n````\r\nfor (auto &element : j) {\r\n  element[\"a\"]=...;\r\n}\r\n````\r\nbetter alternatives or is the version in the readme the optimal usage for all scenarios?\r\n  \r\n(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)\r\n \r\nThanks!\r\n"},"comment":{"url":"https://api.github.com/repos/nlohmann/json/issues/comments/189972802","html_url":"https://github.com/nlohmann/json/issues/214#issuecomment-189972802","issue_url":"https://api.github.com/repos/nlohmann/json/issues/214","id":189972802,"user":{"login":"gregmarr","id":8569738,"avatar_url":"https://avatars.githubusercontent.com/u/8569738?v=3","gravatar_id":"","url":"https://api.github.com/users/gregmarr","html_url":"https://github.com/gregmarr","followers_url":"https://api.github.com/users/gregmarr/followers","following_url":"https://api.github.com/users/gregmarr/following{/other_user}","gists_url":"https://api.github.com/users/gregmarr/gists{/gist_id}","starred_url":"https://api.github.com/users/gregmarr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gregmarr/subscriptions","organizations_url":"https://api.github.com/users/gregmarr/orgs","repos_url":"https://api.github.com/users/gregmarr/repos","events_url":"https://api.github.com/users/gregmarr/events{/privacy}","received_events_url":"https://api.github.com/users/gregmarr/received_events","type":"User","site_admin":false},"created_at":"2016-02-28T23:58:15Z","updated_at":"2016-02-28T23:58:15Z","body":"Yes, that first version will copy the elements.  The second version is better.  The third version is the required form if you want to change the elements of the j object itself.\r\n"}}