This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 112e. See for the official list.


1498. Lifetime of temporaries in range-based for

Section: 8.6.5  [stmt.ranged]     Status: dup     Submitter: Beman Dawes     Date: 2012-04-26

Because the reference __range in the expansion of a range-based for statement, as described in 8.6.5 [stmt.ranged] paragraph 1, is bound only to the top-level expression of range-init, the lifetime of temporaries created at lower levels in that expression expires before the body of the loop is reached, leading to dangling references. It would be helpful if the lifetime of those temporaries were extended over the entire statement.

(See also issue 1523 for another question regarding the rewritten form of the range-based for.)

Rationale (October, 2012):

This is a duplicate of issue 900.