Can a lambda expression be passed as function pointer?

后端 未结 5 1908
忘掉有多难
忘掉有多难 2021-02-05 02:49

I am trying to pass a lambda expression to a function that takes a function pointer, is this even possible?

Here is some sample code, I\'m using VS2010:

         


        
5条回答
  •  有刺的猬
    2021-02-05 03:18

    This works in VS2010:

    template
    void* getcodeptr(const FunctorT& f) {
      auto ptr = &FunctorT::operator();
      return *(void**)&ptr;
    } 
    
    void main() {
      auto hello = [](char* name){ printf("hello %s\n", name); };
      void(*pfn)(char*) = (void(*)(char*)) getcodeptr(hello);
      pfn("world");
    }
    

提交回复
热议问题