A while ago I used std::function pretty much like this:
std::function func = [](int i) -> int { return i; };
Your use case is well-defined according to the standard.
You are constructing a std::function from a callable object[1]
§20.8.11.2.1/7:
templatefunction(F f); Requires: F shall be CopyConstructible. f shall be Callable (20.8.11.2) for argument types ArgTypes and return type R.
So is your f callable?
§20.8.11.2/2 says:
A callable object f of type F is Callable for argument types ArgTypes and return type R if the expres- sion
INVOKE (f, declval, considered as an unevaluated operand (Clause 5), is well formed (20.8.2).()..., R)
And the definition of INVOKE says:
§20.8.2
Define
INVOKE (f, t1, t2, ..., tN)as follows: ... stuff dealing with member function/var pointers ... —f(t1, t2, ..., tN)in all other cases.Define
INVOKE (f, t1, t2, ..., tN, R) as INVOKE (f, t1, t2, ..., tN)implicitly converted toR.
And since any type can be implicitly converted to As pointed out by litb below, there isn't an implicit conversion to void so this isn't well defined.void, your code should be fine with a standards-conforming compiler.
[1]: I think the lambda counts as a callable object here, although I don't have a reference for that. Your lambda could also be used as a function pointer as it captures no context