The only failing case is passing parameters by non-const reference, since temporary variable couldn't be bound to it.
void DrawLine(const Vector& v1, const Vector& v2);
If the object is temporary, why would making the reference const
have any effect on the lifetime of the temporary object?
I guess I also don't fully understand the scope of existence for temporary objects created in an argument.
In the present context, the issue is not the lifetime of the object but whether you can modify it.
Say you make a call.
The object that holds the value
10
in the call should not be modified by the function. If the interface offoo
is:it's fair to implement
foo
as:That would be a problem given the call
foo(10)
. It won't be a problem iffoo
uses aconst&
.From C++11 Standard, Temporary Objects/1
and from C++11 Standard, References/5.2:
A temporary can only bind to a reference to a prvalue. The type of such a reference must be a
const
qualified lvalue reference or a rvalue references.MS Visual Studio compilers have allowed binding of non-
const
references to temporary objects but it is not sanctioned by the standard.