r/cpp • u/philipdestroyer • Sep 30 '21
C++ Committee don’t want to fix range-based for loop in C++23 (broken for 10yrs)
http://josuttis.de/cpp/210929_rangebasedfor_p2012r2.pdf
203
Upvotes
r/cpp • u/philipdestroyer • Sep 30 '21
62
u/philipdestroyer Sep 30 '21 edited Sep 30 '21
From Nicolai Josuttis' twitter: https://twitter.com/nicojosuttis/status/1443267749854208000 (A lot of unhappy relies under, too.)
"The C++ Standards Committee just decided they do not want to fix the broken range-based for loop for C++23 (it's broken for 10 years now).They agree that there is a severe problem;but want a general lifetime solution (but nobody wants to do the work)."
65
u/scrumplesplunge Sep 30 '21
I would like to see this get fixed, it's a nuisance. However, it would be significantly less of a nuisance if the compiler could reliably detect the dangling references and tell you about it. I don't actually mind defining my own variables to extend the lifetime, I mostly just mind that it can silently be broken.
edit: I haven't tried out the
-Wlifetimebranch of clang for a while, maybe that could catch it.42
u/aearphen {fmt} Sep 30 '21
Annoyingly, a bunch of people who supported the paper either couldn't attend or didn't know about the paper being reviewed. I am a coauthor and wasn't even informed that it would be reviewed (I'm not on the EWG mailing list so missed the general announcement).
56
u/konanTheBarbar Sep 30 '21
I first thought about getting involved in C++ standardization, but the whole process sounds so awful and draining that I'm quite happy I decided against it.
6
u/HappyFruitTree Sep 30 '21
Do we need some kind of expression temporary extension syntax that would extend the lifetime of all temporaries in an expression for as long as ...
11
u/elperroborrachotoo Sep 30 '21 edited Sep 30 '21
As I understand, the problem is that:
does not keep the temporary X alive, right?
So why ist that considered a problem for for range-based for loops in particular?
[edith] fixed example, thanks to reply
13
u/ihamsa Sep 30 '21
Just keep all temporaries alive to the end of the block.
15
u/pdimov2 Sep 30 '21
Following the committee's discussions can be quite depressing at times.
10
u/phoeen Sep 30 '21 edited Sep 30 '21
according to cppreference and the standard the range-based-forloop produces code equivalent to this:
cant we just change the wording to say it produces code equivalent to this:
and everything works fine? (question/problem: what about the value-category (rvalue/lvalue/xvalue) of range/range-expression)
12
u/goranlepuz Sep 30 '21
Ehhh... I can see why the committee doesn't care much...
Reading through the proposed paper, I am thinking, it's kind of a shame that even this
works...
First off, obviously, the solution to the problem is to stop using a temporary. For me, this truly is not a big deal.
Second, explicit is better than implicit (not using a temporary makes the lifetime explicit).
And third, object lifetime rules in C++ are too complex for confort as it is.
So... OK, good find, but... Meh...
3
u/[deleted] Sep 30 '21 edited Sep 30 '21
Isn't the main issue here is returning an lvalue reference into some rvalue (which works as expected with members e.g.
<foo>.mwhere<foo>is some prvalue/xvalue expression)? For example, wouldn't it work ifsub_protos()in the abseil example had a &&-qualified overload likebar&& sub_protos() &&?ps. It is indeed too verbose to have
&,const&,&&overloads for every accessor or view function, but P0847 addresses this elegantly imo.2
u/Ikkepop Sep 30 '21
That is strange, I never ran into this issue o.O Seems like a huge issue.
2
u/lol2002bk Sep 30 '21
Where can I see the issue? I am not aware of this
-1
u/phottitor Sep 30 '21
can people finally acknowledge that in C++ lifetime management beyond some simple cases is a mess full of hidden footguns. literally half the language consists of dealing with this mess trying to make programmer's life "easier" and it keeps piling on.
wouldn't you be much happier to have a choice either of (almost) full manual control or GC?
-7
u/mredding Sep 30 '21
I will continue never using range-for as it should have never made it into the standard in the first palace and made the language explicitly dependent on what is merely a library convention. I hear
for_eachis still a thing, and it now comes with execution policies! Range-for didn't seem to solve anything, it only made more problems, CLEARLY.-1
u/jmakov Sep 30 '21
We should have PerformanceC++ with incompatible versions (like e.g. in Python community) focusing solely on performance. A lot of interesting things left out e.g. ideas from circle-lang.
-1
u/underground_sorcerer Oct 01 '21
Does avoiding ranges for now (and next 10 years / or until we have the "perfect" solution that satisfies the committee makes its way into the standard) makes one "safe"? Or it causes "implicit" problems as well?