This is C++, so you have to worry about lifetime. Your question completely ignores lifetime; that is a bad thing.
Whenever you talk about anything in C++ you must be clear about the lifetime of everything involved. One way to be clear about it is to talk about ownership. Another is to say one thing is to talk about scope.
The lifetime of:
void foo(std::function<bool(int i)> f);
f
and copies of it is not clear. If f
and all copies are not going to live past the exit of foo
, that is completely different than if f
or copies could live past the end of foo
.
Part of this is not your fault; the C++ std
library lacks a vocabulary type to describe "callable object with no state ownership" (a function_ref
), so people use the value-semantics std::function
instead.
Supposing what you really want is a function_ref<void(int)>
, then you can just do this:
foo( [&](int i){ return ptr->sampleMethod(i); } );
and done; the scope guarantees (ptr
outlives the call to foo
, and f
and its copies will not outlive foo
) mean lifetime is accounted for.
If however that foo
may keep a copy of f
, you have two problems. First, ptr
claims unique ownership over the lifetime of *ptr
; but f
also claims ownership over the object it wants to call.
What more, std::function
can be copied; which means either the object it invokes must be copied, or we are forced to have shared ownership.
So after foo
does *ptr
need to available outside? Do copies of *ptr
make sense, or shared ownership between the copies of f
of *ptr
? More ownership questions, and the answers to them change what the right solution to your problem is.
In a more perfect world, function
, moveonly_function
, function_ref
would all exist, and your choice would inform what you need here and simplify the solition.