Dukales

save parametres, then call some callable using them

Jun 11th, 2015
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <utility>
  2.  
  3. template< typename ...types >
  4. auto
  5. make_caller(types &&... _values)
  6. {
  7.     return [=] (auto && callee) mutable -> decltype(auto) { return static_cast< decltype(callee) >(callee)(std::forward< types >(_values)...); };
  8. }
  9.  
  10. // main.cpp
  11. #include <iostream>
  12.  
  13. #include <cstdlib>
  14.  
  15. #define PRETTY_PRINT() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
  16.  
  17. struct A
  18. {
  19.  
  20.     template< typename ...types >
  21.     void
  22.     operator () (types...) const &
  23.     {
  24.         PRETTY_PRINT();
  25.     }
  26.  
  27.     template< typename ...types >
  28.     void
  29.     operator () (types...) &&
  30.     {
  31.         PRETTY_PRINT();
  32.     }
  33.  
  34. };
  35.  
  36. void
  37. f(int, float, double, long double)
  38. {
  39.     PRETTY_PRINT();
  40. }
  41.  
  42. struct B
  43. {
  44.    
  45.     template< typename type >
  46.     void
  47.     operator () (type const &) const
  48.     {
  49.         PRETTY_PRINT();
  50.     }
  51.    
  52.     template< typename type >
  53.     void
  54.     operator () (type &&) const
  55.     {
  56.         PRETTY_PRINT();
  57.     }
  58.    
  59. };
  60.  
  61. int
  62. main()
  63. {
  64.     auto caller = make_caller(1, 1.0f, 1.0, 1.0L);
  65.     caller(A{});
  66.     A const a{};
  67.     caller(a);
  68.     caller(f);
  69.     caller(&f);
  70.     make_caller(int{})(B{});
  71.     int const i{};
  72.     make_caller(i)(B{});
  73.     return EXIT_SUCCESS;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment