fork(2) download
  1. #include <iostream>
  2. #include <string>
  3. #include <functional>
  4.  
  5. template <typename T>
  6. void print(T t){
  7. std::cout << t<<std::endl;
  8. }
  9.  
  10. template <typename T>
  11. void print(T t,std::function<void(T& t)> transformer){
  12. transformer(t);
  13. std::cout << t << std::endl;
  14. }
  15.  
  16. template <typename... Params>
  17. void printLabeled(std::string label,Params... params){
  18. std::cout << label << ": ";
  19. print(params...);
  20. }
  21.  
  22. int main() {
  23. //prints the number '1', as expected
  24. print(1);
  25.  
  26. //prints the number the label "answer" and then the number one
  27. printLabeled("answer",1);
  28.  
  29. //I intended this to increment 1 to 2 and then print 2,
  30. //but I guess the lambda does not get converted to an std::function,
  31. //so this doesn't work
  32. //print(1,[](int& num){num+=1;});
  33.  
  34. //This does was the above line was intended to do
  35. print(1,{[](int& num){num+=1;}});
  36.  
  37. //I intended this to be the labeled version of the above line,
  38. //but for some reason, this doesn't work anymore
  39. // printLabeled("answer",1,{[](int& num){num+=1;}});
  40.  
  41. //This labeled version does work finally,
  42. //but its pretty verbose
  43. printLabeled("answer",1,std::function<void(int&)>([](int& num){num+=1;}));
  44. return 0;
  45. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
1
answer: 1
2
answer: 2