char f1();
void f2(char&);
struct A {};
A f3();
void f4(A&);
int main()
{
f2(f1()); // error C2664. This is as expected.
f4(f3()); // OK! Why???
}
error C2664: 'void f4(char &)' : cannot convert argument 1 from 'char' to 'char &'
I have been taught that in C++ a non-const reference parameter cannot be bound to a temporary object; and in the code above, f2(f1());
triggers an error as expected.
However, why does the same rule not apply to the code line f4(f3());
?
PS: My compiler is VC++ 2013. Even if I comment the line f2(f1());
, then the code containing f4(f3());
will be compiled without any errors or warnings.
Update:
MSDN says:
In previous releases of Visual C++, non-const references could be bound to temporary objects. Now, temporary objects can only be bound to const references.
So I think it is a bug of VC++. I have submitted a bug report to VC++ team