From Jason Turner
Watch on YouTube

Summary

This episode explores stateful lambdas in C++11 and C++14, demonstrating how to create lambdas that maintain internal state between invocations. Jason begins by reviewing lambda capture syntax for passing variables by reference and value, explaining the 'mutable' keyword for modifying captured-by-value variables. He then showcases C++14's ability to initialize new variables directly in the capture list, allowing lambdas to maintain state independently of external variables. The episode culminates with a practical example: creating a Fibonacci sequence generator using a stateful lambda that efficiently tracks and updates prior terms.

Resources

Tags