fork download
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. void foo (T &x) {
  5. std::cout << "non-temporary version: " << x.value() << std::endl;
  6. }
  7.  
  8. template <typename T>
  9. void foo (const T &x) {
  10. std::cout << "temporary copied to ";
  11. T copy(x);
  12. foo(copy);
  13. }
  14.  
  15. struct Bar {
  16. const char *value () { return "hi"; }
  17. };
  18.  
  19. int main ()
  20. {
  21. Bar b;
  22. foo(b);
  23. foo(Bar());
  24. return 0;
  25. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
non-temporary version: hi
temporary copied to non-temporary version: hi