Talk:cpp/language/range-for

Use technical detail of standard
The standard says that really "auto __begin = std::Range<_RangeT>::begin(__range)", but I abbreviated this to just "__begin = __range.begin". Do you think this loses accuracy. It improves readability. -- Jaredgrubb 08:33, 2 July 2011 (PDT)

@Jaredgrubb: Are you reading an up-to-date version of the draft? I believe n3242 is the most up-to-date, and I see no mention in it of the  header, nor std::Range. --Benjamin Lindley 12:27, 2 July 2011 (PDT)

Ah, I was using n2243, which I found a link to on the Clang pages. Looks like GCC is based on n2930. I cant find n3242. You have any advice on how to find the one in the "final" proposal - Jaredgrubb 17:49, 2 July 2011 (PDT)
 * Ok here's n3242 . I'll review and fix the text.

Type inference
Does the type inference default to reference or value? I'm not entirely sure of this and it would probably be a good thing to mention on the page. --- Undeterminant 14:00, 30 April 2012 (PDT)


 * If you're referring to the keyword auto, it is explained on auto's page (to put it simple, auto makes the loop variable a copy of the container's element, auto& makes it a reference to the container's element, const auto& makes it a const reference). --Cubbi 19:46, 30 April 2012 (PDT)

auto&& IS NOT "int&&"
this example code comment is wrong: "for (auto&& i : v) // access by reference, the type of i is int&&"

auto&& results in i being "int&" as it is a universal reference

changing "auto&&" to "int&&" results in the expected compiler error

more about why this happens: http://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers
 * You're right of course, good job catching that bug so soon. --Cubbi (talk) 12:01, 19 November 2014 (PST)


 * Why the for(auto &&i : v) instead of for(auto &i : v)? If we add const in there as in for(const auto &&i) then clang complains "error: rvalue reference to type 'const A' cannot bind to lvalue of type 'A'" that makes me think that i is an rvalue reference. But if I take the const out and try to modify i in the loop, then the vector elements get modified, as if i was an lvalue reference to them. Completely confused - any hints as to what's going on? Which should one use: for(auto &&i : v) or for(auto &i : v)? -- CK
 * auto&& is neither lvalue nor rvalue, it's a forwarding reference. Catch by forwarding reference is the most general form (there was even a proposal to make that the default), as mentioned under Notes on this page. When iterating a vector (other than vector ), you can also use regular lvalue references. --Cubbi (talk) 06:21, 15 February 2016 (PST)

Temporary range expression
Should this

''for (auto& x : foo.items) { /* .. */ } // undefined behavior if foo returns by value ''

instead say "undefined behavior if foo.items returns by reference"?


 * no, it's about foo returning by value. --Cubbi (talk) 11:15, 9 June 2020 (PDT)
 * BTW, Serg, thanks for that HOPL-IV link. That's valuable! --Space Mission (talk) 14:31, 10 June 2020 (PDT)

Strange C++23 check in example?
Any reason that we're using to check for C++23 in the example? I've read a lot of C++ and I've never seen anyone write anything like this, and it took me a few moments to comprehend what it was doing in the first place. Is there a reason why shouldn't be preferred? I'm pretty sure there's nothing else like this on this site, and this code won't even work in older compilers/preprocessors which don't support digit separation using. Will eccles (talk) 06:44, 15 July 2022 (PDT)
 * 202300 is fine too, not much reason to prefer one form over the other, call it personal preference --Ybab321 (talk) 08:34, 15 July 2022 (PDT)
 * I would say at the very least that the digit separator ought to be removed, as this code is not valid using older compilers, but I will leave it as-is. Will eccles (talk) 09:04, 15 July 2022 (PDT)
 * Digit separator is C++14 feature (IIRC) and this example contains features that require C++20 anyway, so it is not critical. More generally, examples may contain C++ revision checks or feature tests as a temporary measure, IMO, until on-line compiler supports them. Then such checks would better be removed (maybe leaving only a comment on version) to reduce unnecessary noise. So, I've removed this check in this example, since GCC-12 already supports the using alias declaration in for-loop.
 * made you thinking. Oh, this is good! --Space Mission (talk) 16:32, 15 July 2022 (PDT)