Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

std::bind overloaded class method

20 views
Skip to first unread message

Christopher J. Pisz

unread,
Jan 25, 2017, 8:45:22 AM1/25/17
to
I am trying to try out std::async and futures

In order to do this, I need to bind a class method I would like run
async. The class method in question has overloads. One version is the
top level, another is the recursive portion. etc.

What is the bind syntax?

It usually has worked fine for me with
std::bind(&MyClass::MyMethod, this, std::placeholders::_1);
but now I am dealing with an overloaded method with multiple arguments.

I am trying and failing with:

//------------------------------------------------------------------------------
MyThing::Permutations MyThing::GetPermutations() const
{
std::vector<std::future<Permutations> > futures;
auto func = std::bind<MyThing::Permutations(size_t,
size_t)>(&MyThing::GetPermutations, this, std::placeholders::_1,
std::placeholders::_2);

/*
for( size_t x = 0; x < m_numColumns; ++x )
{
for( size_t y = 0; y < m_numRows; ++y )
{
std::async(std::launch::async, &Board::GetPermutations,
this, x, y);
}
}
*/
GetPermutations(0, 0);
}

with a class that looks like:

//------------------------------------------------------------------------------
class MyThing
{
public:

typedef std::vector<std::string> Permutations;

MyThing();
~MyThing();

Permutations GetPermutations() const;

protected:

typedef std::vector<std::vector<bool> > UsedMap;

Permutations GetPermutations(size_t startingCoordinates_X
, size_t startingCoordinates_Y) const;

Permutations GetPermutations(size_t startingCoordinates_X
, size_t startingCoordinates_Y
, UsedMap usedMap) const;

};

Christopher J. Pisz

unread,
Jan 25, 2017, 9:12:45 AM1/25/17
to
I got it. Twas the const keyword that was missing it seems.

auto func =
std::bind(static_cast<MyThing::Permutations(MyThing::*)(size_t, size_t)
const>(&MyThing::GetPermutations), this, std::placeholders::_2);

Chris Vine

unread,
Jan 25, 2017, 3:19:21 PM1/25/17
to
You disambiguate overloads with static_cast. That was the bit that
made it work for you (unless your original version contained a typing
error so the static_cast was in your real code but omitted from your
posting by mistake). This did of course also require you to get the
signature right, which you did in your second version.

Chris
0 new messages