I am trying to pass variable number of arguments to a lambda function. what is the prototype to accept variable number of arguments in lambda functions? should i write a named function instead of lambda?
std::once_flag flag;
template<typename ...Args>
void gFunc(Args... args)
{
}
template<typename ...Args>
void func(Args... args)
{
std::call_once(flag,[](/*accept variable number of arguments*/... args)
{
// more code here
gFunc( args...);
},
args...
);
}
the below signatures give error:
[&](){ }
[&args](){ }
[&args...](){ }
[&,args...](){ }
[&...args](){ }
In C++14 you can do
auto lambda = [](auto... args) {...};
But in your case I believe simple capture is enough:
std::call_once(flag, [&] {
gFunc(args...); // will implicitly capture all args
}
);
Try this :
template<typename ...Args>
void func(Args... args)
{
std::call_once(flag,[&, args...]( )
{
gFunc( args...);
},
args...
);
}
Stolen from here
§ 5.1.2 A capture followed by an ellipsis is a pack expansion (14.5.3).
[ Example:
template<class... Args>
void f(Args... args) {
auto lm = [&, args...] { return g(args...); };
lm();
}
—end example ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With