I study the generic lambdas, and slightly modified the example,
so my lambda should capture the upper lambda's variadic parameter pack.
So basically what is given to upper lambda as (auto&&...) - should be somehow captured in [=] block.
(The perfect forwarding is another question, I'm curious is it possible here at all?)
#include <iostream>
#include<type_traits>
#include<utility>
// base case
void doPrint(std::ostream& out) {}
template <typename T, typename... Args>
void doPrint(std::ostream& out, T && t, Args && ... args)
{
out << t << " "; // add comma here, see below
doPrint(out, std::forward<Args&&>(args)...);
}
int main()
{
// generic lambda, operator() is a template with one parameter
auto vglambda = [](auto printer) {
return [=](auto&&... ts) // generic lambda, ts is a parameter pack
{
printer(std::forward<decltype(ts)>(ts)...);
return [=] { // HOW TO capture the variadic ts to be accessible HERE ↓
printer(std::forward<decltype(ts)>(ts)...); // ERROR: no matchin function call to forward
}; // nullary lambda (takes no parameters)
};
};
auto p = vglambda([](auto&&...vars) {
doPrint(std::cout, std::forward<decltype(vars)>(vars)...);
});
auto q = p(1, 'a', 3.14,5); // outputs 1a3.14
//q(); //use the returned lambda "printer"
}