I have been using Loki's Functor for a while and I recently asked a question about it (still unanswered...) I have been told to use std::function, but I prefer Loki's implementation of Functor since it also work with all sorts of pointers as parameters (e.g. std::shared_ptr).
struct Toto
{
void foo( int param )
{
std::cout << "foo: " << param << std::endl;
}
};
int
main( int argc, const char** argv )
{
std::shared_ptr<Toto> ptr = std::make_shared<Toto>();
Loki::Functor<void, LOKI_TYPELIST_1(int)> func( ptr, &Toto::foo );
func(1);
}
Is there a way to do so with std::function ?
Use
std::bind
.here,
func
will be deduced to type, that was returned fromstd::bind
or if you don't likeauto
you can use (and you want to usestd::function
)Here
std::function
will be constructed from result ofstd::bind
.ptr
will be copied to some object returned fromstd::bind
, however you can usestd::ref
/std::cref
if you don't want copies.