简体   繁体   中英

std::function and shared_ptr

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 (eg 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 .

auto func = std::bind(&Toto::foo, ptr, std::placeholders::_1);

here, func will be deduced to type, that was returned from std::bind or if you don't like auto you can use (and you want to use std::function )

std::function<void(int)> func = std::bind(&Toto::foo, 
ptr, std::placeholders::_1);

Here std::function will be constructed from result of std::bind . ptr will be copied to some object returned from std::bind , however you can use std::ref / std::cref if you don't want copies.

If you don't want to use std::bind , an option is to use a lambda function, resulting in even smaller code and I personally find it more intuitive:

auto func = [&ptr](int p){ ptr->foo(p); };

or without auto :

std::function<void(int)> func = [&ptr](int p){ ptr->foo(p); };

But this only works if the function to be called is fixed (ie &Toto::foo was not passed dynamically). If not, it's still possible with a lambda but you need a slightly different syntax and std::bind might be more attractive again.

Use std::bind .

struct Toto
{
    void foo( int param )
    {
        std::cout << "foo: " << param << std::endl;
    }
};

int main() {
    std::shared_ptr<Toto> ptr = std::make_shared<Toto>();
    std::function< void(int) > func( std::bind( &Toto::foo,
        std::bind( [ptr] () { return ptr.get(); } ),
        std::placeholders::_1
    ) );

    func( 1 );
}

Live demo.

Edit: The inner bind with a lambda expression is actually unnecessary, but I'll just leave this here as illustrative of more advanced usage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM