mardi 23 janvier 2018

When should I use reference to functions?

I have a vector of std::functions

Using Functors = std::vector<std::function<bool(int)>>;
Functors fs;

Now if I want to add one function to the vector, I do:

fs.emplace_back(
    [](int v)->bool { return v > 1; }
)

I am wondering when I declare "Functors", should I make it reference to std::function instead of raw std::function? It means instead, I will declare like this:

    Using Functors = std::vector<std::function<bool(int)>&>; // note the last &

Thus when I call emplace_back, I would just pass in a reference instead of copy? Or the lambda closure is movable? Please shed some light here: when should I use reference to std::function?

Aucun commentaire:

Enregistrer un commentaire