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

余生颓废 提交于 2019-12-13 01:49:19

问题


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?


回答1:


Given you are using c++11, why not a lambda?

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



回答2:


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



来源:https://stackoverflow.com/questions/34277737/how-to-stdbind-a-smart-pointer-return-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!