Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. void a(int) {
  2. // do something
  3. }
  4. void b(int) {
  5. // something else
  6. }
  7.  
  8.  
  9. void function1() {
  10. a(123);
  11. a(456);
  12. }
  13. void function2() {
  14. b(123);
  15. b(456);
  16. }
  17.  
  18. void test() {
  19. function1();
  20. function2();
  21. }
  22.  
  23. void a(int) {
  24. // do something
  25. }
  26. void b(int) {
  27. // something else
  28. }
  29.  
  30. template<void (*param)(int) >
  31. void function() {
  32. param(123);
  33. param(456);
  34. }
  35.  
  36. void test() {
  37. function<a>();
  38. function<b>();
  39. }
  40.  
  41. template<typename T>
  42. void a(T t) {
  43. // do something
  44. }
  45.  
  46. template<typename T>
  47. void b(T t) {
  48. // something else
  49. }
  50.  
  51. template< ...param... > // ???
  52. void function() {
  53. param<SomeType>(someobj);
  54. param<AnotherType>(someotherobj);
  55. }
  56.  
  57. void test() {
  58. function<a>();
  59. function<b>();
  60. }
  61.  
  62. template <typename T>
  63. struct a {
  64.  
  65. static void foo (T = T ())
  66. {
  67. }
  68.  
  69. };
  70.  
  71. template <typename T>
  72. struct b {
  73.  
  74. static void foo (T = T ())
  75. {
  76. }
  77.  
  78. };
  79.  
  80. struct SomeObj {};
  81. struct SomeOtherObj {};
  82.  
  83. template <template <typename P> class T>
  84. void function ()
  85. {
  86. T<SomeObj>::foo ();
  87. T<SomeOtherObj>::foo ();
  88. }
  89.  
  90. int main ()
  91. {
  92. function<a>();
  93. function<b>();
  94. }
  95.  
  96. template <typename T, T param>
  97. void function() {
  98. param(123);
  99. param(456);
  100. }
  101.  
  102. void test()
  103. {
  104. function< void(*)(int), a<int> >(); // space at end necessary to compiler
  105. function< void(*)(int), b<int> >(); // because the C++ grammar is ambiguous
  106. }
  107.  
  108. #define function(x) do { x<thing1>(obj1); x<thing2>(obj2) } while(0)
  109.  
  110. template<typename T> void a(T t) { /* do something */}
  111. template<typename T> void b(T t) { /* something else */ }
  112.  
  113. template <typename F>
  114. void function(F&& f) {
  115. f(someobj);
  116. f(someotherobj);
  117. }
  118.  
  119. void test() {
  120. // For simple cases, auto&& is even probably auto or const auto&
  121. function([](auto&& t){ a(t); });
  122. function([](auto&& t){ b(t); });
  123.  
  124. // For perfect forwarding
  125. function([](auto&& t){ a(std::forward<decltype(t)>(t)); });
  126. function([](auto&& t){ b(std::forward<decltype(t)>(t)); });
  127. }
  128.  
  129. template < typename F >
  130. void function(F f)
  131. {
  132. f(123);
  133. }
  134.  
  135. void a(int x) { ... }
  136.  
  137. struct b { void operator() (int x) { ... } };
  138.  
  139. void outer()
  140. {
  141. function(&a);
  142. function(b());
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement