fork(1) download
  1. #include <utility>
  2. #include <stdio.h>
  3.  
  4. template <typename R, typename... Args>
  5. struct Helper {
  6. template <R (*Func) (Args...)>
  7. struct Wrapper {
  8. static R call (Args&&... args)
  9. {
  10. return Func(std::forward<Args>(args)...);
  11. }
  12. };
  13. };
  14.  
  15. template <typename R, typename... Args>
  16. struct Helper<R, Args...> MakeHelper (R (*func) (Args...));
  17.  
  18. #define WRAP_FUNC(func) decltype(MakeHelper(func))::Wrapper<func>
  19. #define WRAP_FUNC_T(func) typename decltype(MakeHelper(func))::template Wrapper<func>
  20.  
  21. static double test_func (int x)
  22. {
  23. return x;
  24. }
  25. using TestFunc = WRAP_FUNC(test_func);
  26.  
  27. template <typename Func>
  28. static void CallFunc ()
  29. {
  30. int x = 4;
  31. double r = Func::call(x);
  32. printf("%f\n", r);
  33. }
  34.  
  35. int main ()
  36. {
  37. CallFunc<TestFunc>();
  38. }
  39.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘void CallFunc() [with Func = Helper<double, int>::Wrapper<test_func>]’:
prog.cpp:37:24:   required from here
prog.cpp:31:28: error: cannot bind ‘int’ lvalue to ‘int&&’
     double r = Func::call(x);
                            ^
prog.cpp:8:18: error:   initializing argument 1 of ‘static R Helper<R, Args>::Wrapper<Func>::call(Args&& ...) [with R (* Func)(Args ...) = test_func; R = double; Args = {int}]’
         static R call (Args&&... args)
                  ^
stdout
Standard output is empty