c++ - std::bind 类内部的静态成员函数

标签 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”:对重载函数的调用不明确

感谢任何帮助。

最佳答案

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

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

}

关于c++ - std::bind 类内部的静态成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21401552/

相关文章:

c++ - boost spirit 语义 Action 要求

c++ - 试图将 python 嵌入到 visual studio 2010 c++ 文件中,以代码 1 退出

c++ - 有没有办法让类成员对象的模板调用不同的构造函数?

递归表达式类中的 C++ 运算符重载

c++ - 为什么要重载以前未定义的运算符?

c++ - 为什么必须提供 std::make_shared 类型信息而 std::make_pair 不必提供?

c++ - 为什么标准库常用数学函数不是 "constant expressions"?

c++ - 为什么 InterlockedAdd 在 vs2010 中不可用?

visual-studio - 更改 Visual Studio 的默认设置

c++ - 为什么会调用复制赋值而不是移动赋值?