fork download
  1. #include <iostream>
  2.  
  3. struct foo
  4. {
  5. void bar() { std::cout << "Hello, world!" << std::endl; }
  6. };
  7.  
  8. foo create_foo()
  9. {
  10. return foo();
  11. }
  12.  
  13. int main()
  14. {
  15. // This is perfectly legit even though returned object is an rvalue
  16. create_foo().bar(); // if temporary was const I could not call bar()
  17.  
  18. // This is not possible: r-value cannot be bound to a l-value reference
  19. auto & f = create_foo();
  20. }
Compilation error #stdin compilation error #stdout 0s 3096KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:19:24: error: invalid initialization of non-const reference of type 'foo&' from an rvalue of type 'foo'
  auto & f = create_foo();
                        ^
stdout
Standard output is empty