How can one find out if there is a cost in creating an object by copying a lambda?
template<typename Lambda>
class A {
public:
A(Lambda lambda) : _lambda(lambda) {} // const Lambda& lambda less efficient?
auto call(double x) { return _lambda(x); }
private:
Lambda _lambda; // is const Lambda& _lambda less efficient?
};
I am wondering if having a reference is costly or insignificant (the same as copying the lambda) if the lambda has no state?
References here is going to be ridiculously fragile; you will get bugs if you make that a reference.
Lambdas themselves can choose to be nothing but references to external state; just copy them and store them by value. If there is a performance penalty for this in a specific use case, the caller can avoid the copy cost.
template<class F>
class A {
public:
explicit A(F f_in) : f(std::move(f_in)) {}
decltype(auto) call(double x) { return f(x); }
private:
F f;
};
just a few code-quality changes.