1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <iostream> // Invalid example: void foo(int& x) { std::cout << x << std::endl; } int main() { // not legal; can't bind a temporary to a non-const reference (because what if foo() changed it?) foo(7 * 6); } |
I2luY2x1ZGUgPGlvc3RyZWFtPgoKLy8gSW52YWxpZCBleGFtcGxlOgp2b2lkIGZvbyhpbnQmIHgpCnsKICAgIHN0ZDo6Y291dCA8PCB4IDw8IHN0ZDo6ZW5kbDsKfQoKaW50IG1haW4oKQp7CiAgICAvLyBub3QgbGVnYWw7IGNhbid0IGJpbmQgYSB0ZW1wb3JhcnkgdG8gYSBub24tY29uc3QgcmVmZXJlbmNlIChiZWNhdXNlIHdoYXQgaWYgZm9vKCkgY2hhbmdlZCBpdD8pCiAgICBmb28oNyAqIDYpOwp9
prog.cpp: In function ‘int main()’: prog.cpp:12: error: invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘int’ prog.cpp:4: error: in passing argument 1 of ‘void foo(int&)’
-
result: Compilation error (maybe you wish to see an example for C++ 4.7.2)


