fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3.  
  4. struct X { };
  5.  
  6. template<typename T>
  7. struct aux
  8. {
  9. void operator()(T&&) const { std::cout << 1 << std::endl; }
  10. };
  11.  
  12. template<typename T>
  13. struct aux<const T>
  14. {
  15. void operator()(const T&& t) const { aux<T&>()(t); }
  16. };
  17.  
  18. template<typename T>
  19. struct aux<T&>
  20. {
  21. void operator()(const T&) const { std::cout << 2 << std::endl; }
  22. };
  23.  
  24. template<typename T> void g(T&& t)
  25. {
  26. aux<T>()(std::forward<T>(t));
  27. }
  28.  
  29. X foo() { return X(); }
  30. const X bar() { return X(); }
  31.  
  32. int main()
  33. {
  34. X x1;
  35. g(std::move(x1)); // 1
  36. g(foo()); // 1
  37. g(x1); // 2
  38. g(bar()); // 2
  39.  
  40. const X x2;
  41. g(std::move(x2)); // 2
  42. g(x2); // 2
  43.  
  44. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
1
1
2
2
2
2