dimanche 21 décembre 2014

Using an initializer list of lambdas in a range-based loop

With gcc 4.9 -std=c++14, I tried making a vector of lambdas:



vector<function<void ()>> v = {[]{cout << "foo";}, []{cout << "bar";}};
for (auto&& a: v) a();


And it worked pretty well. Then I tried passing the initializer list of lambdas to the range-based for directly:



for (auto&& a: {[]{cout << "foo";}, []{cout << "bar";}}) a();


And I got:



error: unable to deduce 'std::initializer_list<auto>&&' from '{<lambda closure object>main()::<lambda()>{}, <lambda closure object>main()::<lambda()>{}}'


Judging by the appearance of the error message, I made a wild guess that it is probably because "lambda closure object"s are built-in language terms, and not direct equivalents of std::function (so no real types).


What is the deeper cause of this? Also, could this be implementation-related, or is such behavior dictated by the specification?


Aucun commentaire:

Enregistrer un commentaire