I’ve been thinking of giving a talk at C&B about C++0x lambda functions, because I’ve noticed that they affect the way I write code in C++0x vs. C++98/03 more often than I expected. Lambdas are just syntactic sugar for function objects, which we already had in C++, so it’s somewhat surprising that they actually have a wide-ranging effect on the way we write code in many different kinds of domains. That makes this seems like a topic worth talking about, and that could be of likely interest to lots of people.
I’ve thought about doing a talk about lambdas in the past, but didn’t in part because lambdas haven’t been available in C++ compilers. Two reasons why I think now is the time to write the talk: 1. C++0x lambdas have just become available in several shipping C++ compilers, including Intel C++ 11, Microsoft Visual Studio 2010, and gcc 4.5, so for the first time we can talk about real production code you can write today with real production compilers. 2. Bjarne asked me to put something together to summarize why I think lambdas are so broadly useful, so when I’m done I can send him the slides. :)
The idea of the talk would be to start with a two- or three-slide overview of what lambdas are, then cover examples I’ve encountered in practice that illustrate what makes them an essential and enabling sugar that changes the style and efficiency of how we write code (both for the better):
  • Concurrency and isolation. Making active objects almost seamless to write compared to plain classes, and much simpler than using OO-style and function<>-style implementations.
  • Parallelism and scalability. Making divide-and-conquer and spin-off-chunks-as-tasks code much easier to understand, whether using thread pools or modern ‘task pools’ via work stealing runtimes like TBB/PPL/Cilk.
  • STL programming. This has two aspects — usability, and efficiency. For usability, lambdas make common and important cases incredibly more usable and actually enables following some of Scott’s advice from Effective STL far more often than is practical today. For efficiency and optimization, lambdas can make using std::for_each with a lambda both simpler and more efficient than a screaming fast hand-rolled pointer-chasing for loop. (Don’t just believe my slideware claims; I can do live demonstrations of the code and we can all measure its performance together in real time.)
  • Local functions. Done. They’re as good as in the language.
  • Other small conveniences. For example, making const local variable initialization easy and simple in cases that are inconvenient today.
These examples wouldn’t be only about tediously saying “look ma, there’s another lambda!” over and over. Each example topic above would be a little 10- to 20-minute talk in its own right about that topic, showing motivation and alternatives and then showing how much easier and/or more efficient lambdas make that use case.
What do you think? Interesting? Useful?