Let say you have a class, in which you want to store a function to call at a later time. You want this function to be set at construction time. What I'm not sure of, is what is the right way to declare the parameter in the constructor, and whether or not you should use std::move() when storing it. Example:
class FuncCaller
{
public:
FuncCaller(std::function<int(int)> call_later):m_func(call_later) {}
inline int DoThings(int i) { return m_func(i); }
private:
std::function<int(int)> m_func;
}
float b = 5.3;
FuncCaller c([b](int a){ return (int)(a * b); }
or
ComplexNumber cn(1, 4);
FuncCaller another_one([cn&](int a) { return (int)(cn.rational * a); }
My question is, should the constructor take
std::function<int(int)>
or
std::function<int(int)>
or
std::function<int(int)> &&
Additionally, when capturing the lambda into the member during construction, should I use std::move()
What happens to lambda captures by reference when they are copied?