void func(const std::string& args)
{
// Statically initialize a vector of lambdas (only one here for now)
// The lambdas capture by reference with[&], but since the
// initializer list is static (and thus initialized only once), how
// will the lambda be able to access args with each successive call to func()?
// Won't there be undefined behavior with this?
static std::vector<std::function<void()>> FuncMap =
{
// args (and everything else in scope) will be captured by reference
{ [&]() { for(const auto& s: args) std::cout << s << std::endl; }}
};
auto f = FuncMap[0];
f();
}
void func(const std::string& args) { // Statically initialize a vector of lambdas (only one
Share
I don’t know if you’re asking this out of curiosity, or if you are actually trying to do something like this. If the latter, here’s a possible solution: