1

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?

waas1919
  • 2,365
  • 7
  • 44
  • 76

2 Answers2

4

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

foo.registerCallback([&bar]() { bar.getStuff(); });
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Nim
  • 33,299
  • 2
  • 62
  • 101
  • @Nicol, I left it as copy, as normally you'd want the lifetime to be guaranteed (as it's a callback.) so assuming `bar` is in a `shared_ptr` - which would be the correct model... – Nim Dec 14 '15 at 22:44
  • Yes, but his code bound it explicitly using `std::ref`. If he wanted a copy, he would have just stuck the value in there. Obviously, it's a bad idea to use a reference, but that's what he asked for. – Nicol Bolas Dec 14 '15 at 22:45
0

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

Community
  • 1
  • 1
YSC
  • 38,212
  • 9
  • 96
  • 149