简体   繁体   中英

How to std::bind a smart pointer return method?

So I have this method inside my Bar class:

std::shared_ptr<sf::Sprite> Bar::getStuff() const
{
   //...
}

And I have my callback typedef:

typedef std::function<void()> Callback;

void Foo::registerCallback(const Callback& callback)
{
    //...
}

And now I want to use std::bind on this method, like:

Foo foo;
Bar bar; //I create an instance of an Bar, called bar. 

foo.registerCallback(std::bind(&Bar::getStuff, std::ref(bar))); //<--- ERROR!!!!

ERROR:

error C2562: 'std::_Callable_obj<std::_Bind<true,std::shared_ptr<sf::Sprite>,std::_Pmf_wrap<std::shared_ptr<sf::Sprite> (__thiscall Bar::* )(void) 

If I want to use a void method, it's ok. But I need to use the getStuff() method, which will return me a smart pointer to a sf::Sprite thing.

How can I achieve this?

鉴于您正在使用c ++ 11,为什么不使用lambda?

foo.registerCallback([&bar]() { bar.getStuff(); });

std::bind works with std::reference_wrapper . Your error comes from the way you call it. Bar::getStuff returns something you try to discard. You can only discard things in discard expressions and statements.

A solution

template<class Callable>
auto discard_callable(Callable&& callable)
{ return [=]() { (void) callable(); }; }

You could use it like this:

foo.registerCallback(discard_callable(
    std::bind(&Bar::getStuff, std::cref(bar))
));

MCVE

#include <functional>
#include <string>

struct Butler
{
    std::string say() const { return "Welcome home, Sir."; }
};

template<class Callable>
auto discard_callable(Callable&& callable)
{ return [=]() { (void) callable(); }; }

int main()
{
    Butler igor;
    std::function<void()> welcome = discard_callable(std::bind(&Butler::say, std::ref(igor))); // error without discard
    welcome();
}

Live demo

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