func_val(--getfoo()); // #1 OK?
Yes, OK. The operator--
is a member-function, which is called, and which returns itself (and lvalue referring to itself). The object is then copied into the parameter of func_val
. Notice that return value optimization is not allowed to apply, since the temporary created by getfoo()
was previously bound to a reference.
func_ref(getfoo()); // #2 OK?
Yes, OK. The call getfoo()
returns a temporary which is bound to the const reference. A copy constructor is required, but the call it it may be optimized out by the implementation. The temporary persists until the end of the full-expression containing the call to func_ref
(the whole expression statement here).
func_ref(getfoo().ref());
Yes, OK. No copy constructor required, as we bind the const reference not to a temporary but to the lvalue representing the object itself.
// the following line is a real example
// using --vector.end() instead of --getfoo()
That is not required to work. Think of a situation where vector.end()
returns a T*
(allowed). You are not allowed to modify rvalues of non-class type, so in that case, this would be ill-formed.
func_ref(--getfoo());
Yes, OK. The argument is evaluated as in #1
, but the resulting lvalue is directly passed and the const reference is bound to it. In this sense it's equal to #3
(except for the decrement side effect).
const Foo & xref = --getfoo();
The Standard wording is not entirely clear. It surely intends to only extend lifetime of objects not yet bound to a reference. But in our case, --getfoo()
yields an lvalue refering to a temporary object which was previously bound to a reference. It may be worth submitting a defect report to the committee (i may also have missed wording that requires the temporary object to not be bounded to a reference yet).
In any case, the intended behavior is to destruct the temporary that results from getfoo()
at the end of initializing xref
, so xref
will become a dangling reference.
The thing is that operator-- returns some reference and the compiler does not see any relation between this reference and the temporary we created.
Exactly (but applies only to the initialization of xref
which will go mad. In all other cases, the intended behavior you want (or what i believe you want) is achieved).