C++ lambda with captures as a function pointer

后端 未结 8 890
离开以前
离开以前 2020-11-22 15:35

I was playing with C++ lambdas and their implicit conversion to function pointers. My starting example was using them as callback for the ftw function. This works as expecte

8条回答
  •  無奈伤痛
    2020-11-22 15:46

    Found an answer here: http://meh.schizofreni.co/programming/magic/2013/01/23/function-pointer-from-lambda.html

    It converts lambda pointer to void* and convert back when needed.

    1. to void*:

      auto voidfunction = new decltype(to_function(lambda))(to_function(lambda));

    2. from void*:

      auto function = static_cast< std::function*>( voidfunction);

提交回复
热议问题