5 ms·
Another mildly obscure feature of lambdas is the ability to capture a variadic number of parameters. Example (slightly contrived): #include <future> #incl
by hendzen 10y ago
Another mildly obscure feature of lambdas is the ability to capture a variadic number of parameters.
Example (slightly contrived):
#include <future>
#include <iostream>
template<typename... Args>
void log(Args&&... args) {
(std::cout << ... << args) << std::endl;
}
template<typename... Args>
std::future<void> log_async(Args&&... args) {
return std::async(std::launch::async, [args...] { log(args...); });
}
int main()
{
auto f = log_async(1, 2, 3);
f.wait();
}
- netheril96 10y agoWhich standard version?
- hendzen 10y agoThat snippet depends on fold expressions, which are in c++17. AFAIK, capturing a variadic parameter pack should work in C++11.
- groovy2shoes 10y agoFor those of us still stuck on C++98 at work, would you mind explaining what's going on here? In particular, I can't figure out why the ellipsis is so separated from `args` here: (std::cout << ... << args) << std::endl; That looks like some black magic to me. The rest makes sense, I think.
- hendzen 10y agohttp://en.cppreference.com/w/cpp/language/fold http://en.cppreference.com/w/cpp/language/fold
- groovy2shoes 10y agoNeat! Thank you :)
- makecheck 10y agoHeh, great. I used to put an ellipsis into a programming example to mean “fill in whatever you actually do here”, and now C++ went and made it mean something. :)
- moonshinefe 10y agoWell, I feel like an old man telling kids to get off my lawn, but this syntax looks ridiculous. :/