相关疑难解决方法(0)

C++ 0x lambda用于makecontext参数#2

我无法将C++ 0x lambda函数作为第二个参数传递给makecontext(来自ucontext.h).签名makecontext是:

void makecontext(ucontext_t*, void (*)(), int, ...);
Run Code Online (Sandbox Code Playgroud)

以前,我能够将C风格的(void (*)(void))强制转换应用到我使用的全局范围函数中.A reinterpret_cast可以用C++来解决问题.但是,使用C++ 0x lambda函数,我收到以下错误:

error: invalid cast from type ‘main(int, char**)::<lambda(int)>’ to type ‘void (*)()’
Run Code Online (Sandbox Code Playgroud)

我正在使用G ++ 4.6.以下代码足以产生编译错误:

#include <ucontext.h>

void f1(int i) {}

int main(int argc, char *argv[]) {
  ucontext_t c;
  makecontext(&c, (void (*)(void))f1, 1, 123); // ok
  makecontext(&c, reinterpret_cast<void (*)(void)>(f1), 1, 123); // ok

  auto f2 = [](int i){};
  makecontext(&c, (void (*)(void))f2, 1, 123); // error
  makecontext(&c, reinterpret_cast<void (*) (void)>(f2), 1, …
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++11

5
推荐指数
1
解决办法
791
查看次数

标签 统计

c++ ×1

c++11 ×1

lambda ×1