Why can't I create a vector of lambdas (of the same type) in C++11?

C++VectorLambdaC++11

C++ Problem Overview


I was trying to create a vector of lambda, but failed:

auto ignore = [&]() { return 10; };  //1
std::vector<decltype(ignore)> v;     //2
v.push_back([&]() { return 100; });  //3

Up to line #2, it compiles fine. But the line#3 gives compilation error:

>error: no matching function for call to 'std::vector<main()::<lambda()>>::push_back(main()::<lambda()>)'

I don't want a vector of function pointers or vector of function objects. However, vector of function objects which encapsulate real lambda expressions, would work for me. Is this possible?

C++ Solutions


Solution 1 - C++

Every lambda has a different type—even if they have the same signature. You must use a run-time encapsulating container such as std::function if you want to do something like that.

e.g.:

std::vector<std::function<int()>> functors;
functors.push_back([&] { return 100; });
functors.push_back([&] { return  10; });

Solution 2 - C++

All lambda expressions have a different type, even if they are identical character-by-character. You're pushing a lambda of a different type (because it's another expression) into the vector, and that obviously won't work.

One solution is to make a vector of std::function<int()> instead.

auto ignore = [&]() { return 10; };
std::vector<std::function<int()>> v;
v.push_back(ignore);
v.push_back([&]() { return 100; });

On another note, it's not a good idea to use [&] when you're not capturing anything.

Solution 3 - C++

While what others have said is relevant, it is still possible to declare and use a vector of lambda, although it's not very useful:

auto lambda = [] { return 10; };
std::vector<decltype(lambda)> vec;
vec.push_back(lambda);

So, you can store any number of lambdas in there, so long as it's a copy/move of lambda!

Solution 4 - C++

If your lambda is stateless, i.e., [](...){...}, C++11 allows it to degrade into a function pointer. In theory, a C++11 compliant compiler would be able to compile this:

auto ignore = []() { return 10; };  //1 note misssing & in []!
std::vector<int (*)()> v;     //2
v.push_back([]() { return 100; });  //3

Solution 5 - C++

You could use a lambda generating function (updated with fix suggested by Nawaz):

#include <vector>
#include <iostream>

int main() {
    auto lambda_gen = [] (int i) {return [i](int x){ return i*x;};} ;

    using my_lambda = decltype(lambda_gen(1));

    std::vector<my_lambda> vec;

    for(int i = 0; i < 10; i++) vec.push_back(lambda_gen(i));

    int i = 0;

    for (auto& lambda : vec){
        std::cout << lambda(i) << std::endl;
        i++;
    }
}

But I think you basically made your own class at this point. Otherwise if the lambdas have completely different caputres/args etc. you probably have to use a tuple.

Solution 6 - C++

Each lambda is a different type. You must use std::tuple instead of std::vector.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNawazView Question on Stackoverflow
Solution 1 - C++PuppyView Answer on Stackoverflow
Solution 2 - C++R. Martinho FernandesView Answer on Stackoverflow
Solution 3 - C++Luc DantonView Answer on Stackoverflow
Solution 4 - C++MSNView Answer on Stackoverflow
Solution 5 - C++antediluvianView Answer on Stackoverflow
Solution 6 - C++Paul Fultz IIView Answer on Stackoverflow