g++ gives a different and more instructive warning and error when foo is not a member:
g++ -c -pipe -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -o main.o main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.
cpp:20:46: error
: invalid initialization of non
-const reference of type ‘QString
&’ from an rvalue of type ‘
QString’
main.cpp:8:6: error: in passing argument 4 of ‘bool foo(class1*, int, int, QString&, QString&, class2*)’
make: *** [main.o] Error 1
g++ -c -pipe -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -o main.o main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:20:46: error: invalid initialization of non-const reference of type ‘QString&’ from an rvalue of type ‘QString’
main.cpp:8:6: error: in passing argument 4 of ‘bool foo(class1*, int, int, QString&, QString&, class2*)’
make: *** [main.o] Error 1
To copy to clipboard, switch view to plain text mode
I believe the Microsoft compiler will issue an error with Disable Language Extensions turned on, and a warning at /W4. Apparently a lot of existing code uses non-const references to rvalues so changing the default behaviour would break things. Presumably the rvalue is being used when you (occasionally) don't care about the value that the function would return in the referenced parameter.
Bookmarks