9

I am trying to store a function to call later, here is a snippet.

This works fine:

void RandomClass::aFunc( int param1, int param2, double param3, bool isQueued /*= false */ )
{
    /* If some condition happened, store this func for later */
    auto storeFunc = std::bind (&RandomClass::aFunc, this, param1, param2, param3, true);

    CommandList.push( storeFunc );

    /* Do random stuff */
}

However, if RandomClass is static, so I believe I should do this:

void RandomClass::aFunc( int param1, int param2, double param3, bool isQueued /*= false */ )
{
    /* If some condition happened, store this func for later */
    auto storeFunc = std::bind (&RandomClass::aFunc, param1, param2, param3, true);

    CommandList.push( storeFunc );

    /* Do random stuff */
}

But this doesn't work, I get the compile error

error C2668: 'std::tr1::bind' : ambiguous call to overloaded function

Any help appreciated.

S Grey
  • 589
  • 2
  • 7
  • 12
  • 2
    Is there more than one overload for `RandomClass::aFunc`? – juanchopanza Jan 28 '14 at 09:26
  • Also I could do this which works for a normal class, but not the static version. CommandList.push( [=] () { aFunc(param1, param2, param3, true); }); – S Grey Jan 28 '14 at 09:36
  • Yes there is a second ::aFunc with different parameters. The other function uses strings and has less parameters compared to the one i'm trying to use. – S Grey Jan 28 '14 at 09:39
  • possible duplicate of [std::bind overload resolution](http://stackoverflow.com/questions/4159487/stdbind-overload-resolution) – juanchopanza Jan 28 '14 at 09:39
  • OK, I found a duplicate. I hope it is helpful. The bottom line is you have to cast to the right overload type. – juanchopanza Jan 28 '14 at 09:40
  • I tried that solution on the sample, non static class and it worked. But It gives over 100 errors on my static class :( Originally the states that the signatures do not match, so you was right about the second method causing a problem. But because the class is static the new solution you linked just gives garbage errors. – S Grey Jan 28 '14 at 09:56
  • You need to find the right type of function pointer for a static member function. Also, there is no such thing as a static class in C++. – juanchopanza Jan 28 '14 at 09:58
  • I thought it would be best to post my syntax, can you please see here? http://pastebin.com/pnr4SbFV No doubt its something silly. – S Grey Jan 28 '14 at 10:12
  • OK, your syntax was wrong. I added an answer. – juanchopanza Jan 28 '14 at 10:29

1 Answers1

22

The type of a pointer to static member function looks like a pointer to a non-member function:

auto storeFunc = std::bind ( (void(*)(WORD, WORD, double, bool))
                              &CSoundRouteHandlerApp::MakeRoute, 
                              sourcePort, destPort, volume, true );

Here's a simplified example:

struct Foo
{
  void foo_nonstatic(int, int) {}
  static int foo_static(int, int, int) { return 42;}
};

#include <functional>
int main()
{
  auto f_nonstatic = std::bind((void(Foo::*)(int, int))&Foo::foo_nonstatic, Foo(), 1, 2);
  auto f_static = std::bind((int(*)(int, int, int))&Foo::foo_static, 1, 2, 3);

}
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • I can't upvote you or mark it as the answer for some reason.. Please mark this as correct if anyone can. – S Grey Jan 28 '14 at 11:31
  • @StaetonGrey I think you can mark it as an answer, but you need more rep points to up-vote. – juanchopanza Jan 28 '14 at 11:31
  • Oh silly me, I expected some obvious "Accept as Answer" button, but its just clicking the tick graphic :) Yes, no upvotes until I get 15Rep, sorry! – S Grey Jan 28 '14 at 11:40
  • 2
    Is there an advantage of stating the return and arguments types explicitly in the call of `std::bind`? (or is it just to help explain what is happening) – Roi Danton Aug 05 '17 at 18:44