[diff.cpp20.stmt]

# Annex C (informative) Compatibility [[diff]](./#diff)

## C.2 C++ and ISO C++ 2020 [[diff.cpp20]](diff.cpp20#stmt)

### C.2.4 [[stmt]](stmt "8 Statements"): statements [diff.cpp20.stmt]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/compatibility.tex#L533)

**Affected subclause:** [[stmt.ranged]](stmt.ranged)  


**Change:** The lifetime of temporary objects in the [*for-range-initializer*](stmt.pre#nt:for-range-initializer "8.1 Preamble [stmt.pre]") is extended until the end of the loop ([[class.temporary]](class.temporary "6.8.7 Temporary objects"))[.](#1.sentence-1)

  


**Rationale:** Improve usability of the range-based for statement[.](#1.sentence-2)

  


**Effect on original feature:** Destructors of some temporary objects are invoked later[.](#1.sentence-3)

[*Example [1](#example-1)*: void f() { std::vector<int> v = { 42, 17, 13 };
 std::mutex m; for (int x :static_cast<void>(std::lock_guard<std::mutex>(m)), v) { // lock released in C++ 2020 std::lock_guard<std::mutex> guard(m); // OK in C++ 2020, now deadlocks}} â *end example*]
