Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. void func()
  4. {
  5. std::cout << "Hello";
  6. }
  7.  
  8. auto t = []{ func(); };
  9. typedef decltype(t) functor_type;
  10.  
  11. template <class F>
  12. void functor_caller()
  13. {
  14. F f;
  15. f();
  16. }
  17.  
  18. int main()
  19. {
  20. functor_caller<functor_type>();
  21. return 0;
  22. }
  23.  
  24. error: use of deleted function '<lambda()>::<lambda>()'
  25.  
  26. error: a lambda closure type has a deleted default constructor
  27.  
  28. #define WRAP_FUNC(f)
  29. struct f##_functor
  30. {
  31. template <class... Args >
  32. auto operator()(Args ... args) ->decltype(f(args...))
  33. {
  34. return f(args...);
  35. }
  36. };
  37.  
  38. WRAP_FUNC(func);
  39.  
  40. functor_caller<func_functor>()
  41.  
  42. {
  43. int n = 0;
  44. auto t = [&n](int a) -> int { return n += a; };
  45. }
  46.  
  47. std::function<int(int)> F = t;
  48.  
  49. template <typename F>
  50. int compute(int a, int b, F f)
  51. {
  52. return a * f(b); // example
  53. }
  54.  
  55. template<class F>
  56. struct wrapper
  57. {
  58. static_assert(std::is_empty<F>(), "Lambdas must be empty");
  59. template<class... Ts>
  60. auto operator()(Ts&&... xs) const -> decltype(reinterpret_cast<const F&>(*this)(std::forward<Ts>(xs)...))
  61. {
  62. return reinterpret_cast<const F&>(*this)(std::forward<Ts>(xs)...);
  63. }
  64. };
  65.  
  66. template <class F>
  67. void function_caller()
  68. {
  69. wrapper<F> f;
  70. f();
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement