fork(1) download
  1. #include <iostream>
  2.  
  3. class Foo {
  4. public:
  5. Foo() {
  6. std::cout << "constructed\n";
  7. }
  8. ~Foo() {
  9. std::cout << "destructed\n";
  10. }
  11. };
  12.  
  13. Foo getFoo() {
  14. return Foo();
  15. }
  16.  
  17. void fz(Foo& f) {
  18. }
  19.  
  20. int getInt() {
  21. return int();
  22. }
  23.  
  24. void iz(int& i) {
  25. }
  26.  
  27. int main() {
  28. {
  29. Foo& z = getFoo(); //ok
  30. fz(getFoo()); //ok
  31.  
  32. int& z2 = getInt(); //error: initial value of reference to non-const must be an lvalue
  33. iz(getInt()); //same as above
  34. }
  35. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:29:21: error: invalid initialization of non-const reference of type ‘Foo&’ from an rvalue of type ‘Foo’
prog.cpp:30:16: error: invalid initialization of non-const reference of type ‘Foo&’ from an rvalue of type ‘Foo’
prog.cpp:17:6: error: in passing argument 1 of ‘void fz(Foo&)’
prog.cpp:32:22: error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
prog.cpp:33:16: error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
prog.cpp:24:6: error: in passing argument 1 of ‘void iz(int&)’
prog.cpp:29:10: warning: unused variable ‘z’ [-Wunused-variable]
prog.cpp:32:10: warning: unused variable ‘z2’ [-Wunused-variable]
stdout
Standard output is empty