Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. template<typename F, typename... args, typename R>
  5. static R unsafe_c_callback(void* thisptr,args... a) {
  6.     return (*((F*)thisptr))(a...);
  7. }
  8.  
  9.  
  10.  
  11. template<typename F, typename... args, typename R>
  12. static void* C(const F& callback, R(*&fptr)(void*,args...)) {
  13.     fptr = unsafe_c_callback<F,args...>;
  14.     return (void*)&callback;
  15. }
  16. int main(int argc, char** argv) {
  17.  
  18.     auto bot = [=](const char* txt){
  19.         printf("%s\n",txt);
  20.         return 5;
  21.     };
  22.     int(*fptr)(void*,const char*);
  23.     void* thisptr = C(bot,fptr);
  24.     int rval = fptr(thisptr,"Lambda text");
  25.     printf("%i\n",rval);
  26. return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement