std ::绑定类中的静态成员函数

时间:2014-01-28 09:25:00

标签: c++ visual-studio-2010 c++11

我正在尝试存储一个稍后调用的函数,这是一个片段。

这很好用:

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 */
}

但是,如果RandomClass是静态的,那么我相信我应该这样做:

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 */
}

但这不起作用,我得到了编译错误

错误C2668:'std :: tr1 :: bind':对重载函数的模糊调用

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:16)

指向静态成员函数的指针类型看起来像指向非成员函数的指针:

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

这是一个简化的例子:

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);

}