As others have said, you can take a constant, but not a non-constant reference to a temporary one.
In practice, there may be dangers in providing non-constant references to temporary:
#include <iostream> void foo(int &i) { i = 5; } int main() { long l = 4; foo(l); std::cout << l << "\n"; }
Now l can be implicitly converted to int , therefore, if a reference was not allowed to a constant, then presumably foo will be passed a reference to the result of this conversion, the same as in fact, if foo accepts const int & . The assignment will be performed in the temporary, and then discarded when the temporary is destroyed. This is much more likely to be a confusing mistake than this should be the intended result.
I donβt know if there is a clear set of rules allowing to refer to non-constant links to temporary situations in some situations, but not dangerous / annoying, but even if the C ++ standard did not include them. Note that C ++ 0x has rvalue links that allow you to perform some additional actions with temporary parameters that cannot be performed in C ++ 03.
source share