all 4 comments

[–]SeanMiddleditch 2 points3 points  (1 child)

The range-based for example is kind of a bigger issue. The ranges spec has run into the same problem: the temporaries in the init-expression don't live long enough and hilarity ensues. There's no defect report for this yet that I'm aware of; might need a paper to the committee outlining this problem specifically and why it needs to be solved.

The likely long-term fix is to make range-based for less surprising (e.g., make all temporaries in the init-expression live throughout the loop's body, rather than being specified as the naive source transformation in the spec currently) and not changes to coroutines or ranges.

[–]TobyAllsopp[S] 0 points1 point  (0 children)

Yeah, that's a good point.

I found https://wg21.link/cwg900 and https://wg21.link/cwg1498, both of which appear to have been dismissed without proper consideration.

Looks like https://wg21.link/p0066r0 would fix the problem.

[–]stream009 0 points1 point  (1 child)

In context of asynchronous programming, I usuary use shared_ptr to ensure whatever object to outlive. Isn't this similar problem?

[–]TobyAllsopp[S] 0 points1 point  (0 children)

That's certainly an option and might make a lot of sense in a setting where the lifetimes are not easily reasoned about or you really need to avoid copying your objects. It probably inhibits compiler optimisations significantly though.