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