Reader Q&A: Generic lambdas
Tim just added this comment on the GotW #3 Solution blog post from last year:
Are you sure you can use auto in lambda like this?
I can not compile the code and I’m pretty sure auto does not work here.
If you mean auto as a lambda parameter type, such as
[](auto& s){ use(s); }
then yes, it’s (now) legal): That’s a new feature in currently-being-finalized C++14 standard, and it’s called “generic lambdas.” It means that the compiler-generated closure object’s
operator()
is a template, so you can call the same closure object multiple times with different types and get the templated operator stamped out for each set of types it’s called with.
Major compilers are now adding support for this. As of this writing, all of GCC, Clang, and Visual C++ have implemented the basic feature and you can get it in CTP/preview/alpha releases of each, such as GCC or Clang trunk, or Visual C++ November 2013 CTP. I can’t remember offhand which of those compilers have shipped an official release since adding it (VC++ has not) but they’ll all have it in their next released versions.
By the way, isn’t it wonderful that, for the first time in the history of C++, multiple major compilers are in pretty good sync like this, both with each other and with the standard? I think that’s awesome.
Filed under: Uncategorized

Herb Sutter's Blog
- Herb Sutter's profile
- 32 followers
