std ::在类内绑定静态成员函数 [英] std::bind a static member function inside the class

查看:500
本文介绍了std ::在类内绑定静态成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这很好:

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是静态的,那么我相信我应该这样做:

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

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

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

感谢任何帮助。

推荐答案

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

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

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

}

这篇关于std ::在类内绑定静态成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆