fork(1) download
  1. #include <iostream>
  2. #include <functional>
  3. #include <utility>
  4.  
  5. void foo(std::greater<int>& t)
  6. {
  7. std::cout << "foo called on T&\n";
  8. }
  9.  
  10. void foo(const std::greater<int>& t)
  11. {
  12. std::cout << "foo called on const T&\n";
  13. }
  14.  
  15. void foo(std::greater<int>&& t)
  16. {
  17. std::cout << "foo called on T&&\n";
  18. }
  19.  
  20. void foo(const std::greater<int>&& t)
  21. {
  22. std::cout << "foo called on const T&&\n";
  23. }
  24.  
  25. template <class Function = std::greater<int> >
  26. void f(Function&& f = Function())
  27. {
  28. foo(std::forward<Function>(f));
  29. }
  30.  
  31. int main()
  32. {
  33. std::greater<int> g = std::greater<int>();
  34. const std::greater<int> cg = std::greater<int>();
  35. f();
  36. f(g);
  37. f(cg);
  38. f(std::greater<int>());
  39. f(const_cast<const std::greater<int>&&>(std::greater<int>()));
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
foo called on T&&
foo called on T&
foo called on const T&
foo called on T&&
foo called on const T&&