如何将boost绑定转换为C函数指针?

Cia*_*tic 13 c++ boost

假设我有这个:

void func(WCHAR* pythonStatement) {
  // Do something with pythonStatement
}
Run Code Online (Sandbox Code Playgroud)

我需要将它转换为void function(void),如下所示:

bind(func, TEXT("console.write('test')"))
Run Code Online (Sandbox Code Playgroud)

现在我有这样的结构:

typedef void (__cdecl * PFUNCPLUGINCMD)();

struct FuncItem {
PFUNCPLUGINCMD pFunc;
    // ...
};
Run Code Online (Sandbox Code Playgroud)

如何设置我的struct的pFunc bind(func, "something")?绑定返回lambda_functor而不是函数指针,那么如何将此函子转换为函数指针?

谢谢.


结束使用包装"解决方案"(GitHub)

Sim*_*one 6

我认为你不能,除非你使得lamba_functor成为一个全局变量.

在这种情况下,您可以声明一个调用它的函数:

void uglyWorkaround() {
    globalLambdaFunctor();
}
Run Code Online (Sandbox Code Playgroud)

并设置pFuncuglyWorkaround().

编辑
只是一个旁注:如果你将静态文本绑定到函数调用,你可以完全省略bind()调用并写入:

void wrapper() {
    func(TEXT("console.write('test')"));
}
Run Code Online (Sandbox Code Playgroud)

并设置pFuncwrapper().

  • +1:这是此问题的标准解决方法.通常,您的回调可以接收`void*`作为参数,以传递上下文.[本常见问题解答](http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.2)更详细地解释了它. (3认同)