fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class X {};
  5. X f() { return X(); } // Return by value
  6. void g1(X&) {} // Pass by non-const reference
  7. void g2(const X&) {} // Pass by const reference
  8. int main() {
  9. // Error: const temporary created by f():
  10. cout<<"Hey";
  11. g1(f());
  12. // OK: g2 takes a const reference:
  13. g2(f());
  14. } ///:~
Compilation error #stdin compilation error #stdout 0s 3140KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:11:8: error: invalid initialization of non-const reference of type 'X&' from an rvalue of type 'X'
  g1(f());
        ^
prog.cpp:6:6: note: in passing argument 1 of 'void g1(X&)'
 void g1(X&) {} // Pass by non-const reference
      ^
stdout
Standard output is empty