Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. // C++11
  2. #include <functional>
  3. using fp = std::function<int()>;
  4.  
  5. fp f1(int i)
  6. {
  7. i *= 100
  8. return [i]() mutable { return i++; };
  9. }
  10.  
  11. fp f2(int a, int b)
  12. {
  13. return [a, b]() mutable { a++; b--; return a * b; };
  14. }
  15.  
  16. int main()
  17. {
  18. fp p;
  19. int i;
  20.  
  21. p = f1(1);
  22. i = p();
  23. i = p();
  24. i = p();
  25.  
  26. p = f2(3, 5);
  27. i = p();
  28. i = p();
  29. i = p();
  30.  
  31. return 0;
  32. }
  33.  
  34. fp f(int i)
  35. {
  36. i *= 100
  37. return [i]() mutable { return i++; };
  38. }
  39.  
  40. #include <iostream>
  41. #include <functional>
  42.  
  43. using fp = std::function<int()>;
  44.  
  45. fp f(int i)
  46. {
  47. i *= 100;
  48.  
  49. struct A
  50. {
  51. A(int i) : i(i) {}
  52. int i;
  53.  
  54. int operator ()() { return i++; }
  55. };
  56.  
  57. return A( i );
  58. }
  59.  
  60. int main()
  61. {
  62. fp p;
  63. int i;
  64.  
  65. p = f(1);
  66. std::cout << ( i = p() ) << std::endl;
  67. std::cout << ( i = p() ) << std::endl;
  68. std::cout << ( i = p() ) << std::endl;
  69. }
  70.  
  71. 100
  72. 101
  73. 102
  74.  
  75. class __closure {
  76. public:
  77. __closure(const __closure&) = default;
  78. ~__closure() = default;
  79.  
  80. int operator()();
  81.  
  82. private:
  83. __closure(unspecified arguments);
  84. __closure& operator=(const __closure&) = default;
  85. __closure() = delete;
  86.  
  87. int i;
  88. };
  89.  
  90. class __closure {
  91. using signature = int();
  92. static int body();
  93. public:
  94. int operator() { return body(); }
  95. operator signature*() { return &body; }
  96. ...
  97.  
  98. class Lambda
  99. {
  100. захваченные переменные
  101.  
  102. type operator()(params) { ... };
  103.  
  104. Lambda L;
  105.  
  106. L();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement