In my case an exception seems very unlikely but is it possible at all ?
In principle, yes. std::function
will have to allocate memory to store the callable object it's initialised with, and if that memory is allocated dynamically then there's the possibility of failure.
In practice, in your case, no. In the words of a note in the specification, "Implementations are encouraged to avoid the use of dynamically allocated memory for small callable objects". A lambda with no captures is convertible to a function pointer, which is about as small as a callable object gets; so a good implementation should certainly store that without dynamic allocation. And of course, copying a pointer can't throw either.
Larger objects (including lambdas with many captures) will need dynamic allocation, and need to copy their captured objects or other state, and so can't offer a no-throw guarantee.
std::function
uses internal magic like type erasure, I think it always has to deal with the possibility of allocation failures. I guess you could implement a "small member optimization" for type erasure, but that might be extreme. – Kerrek SBvoid*
(for the object pointer). – Xeo