From Jason Turner
Watch on YouTube

Summary

This episode explores C++20's new for loop init-statements, an extension to range-based for loops that allows variable declarations before the loop condition. Jason demonstrates how this feature enables both tracking position within a range-based loop and, more importantly, solving object lifetime issues. By placing temporary objects in the init-statement, their lifetime extends throughout the entire loop, preventing dangling references that could otherwise occur when iterating over temporary collections returned from functions.

Resources

Tags