I want a way to make functor from function. Now I trying to wrap function call by lambda function and instantiate it later. But compiler says than lambda constructor is dele
The code doesn't make sense. Imagine you have a capturing lambda like this:
{
int n = 0;
auto t = [&n](int a) -> int { return n += a; };
}
What could it possibly mean to default-construct an object of type decltype(t)?
As @Matthieu suggests, you could wrap the lambda into a function object:
std::function F = t;
Or you could template your call-site directly on the type of the lambda (or any callable entity):
template
int compute(int a, int b, F f)
{
return a * f(b); // example
}
Usage: int a = 0; for (int i : { 1, 3, 5 }) { a += compute(10, i, t); }
If at all possible, the second style is preferable, since the conversion to std::function is a non-trivial, potentially expensive operation, as is the actual function call through the resulting object. However, if you need to store a uniform collection of heterogeneous callable entities, then std::function may well be the easiest and most convenient solution.