The line foo(bar())
is trying to bind an rvalue of type s
(the temporary returned by bar()
) to a non-const
lvalue reference to s
(the argument of foo()
). This is illegal, hence the compilation error. rvalues can only be bound to rvalue references or to const
lvalue references.
The expression +bar()
, on the other hand, returns an lvalue reference (that's the return type of operator + ()
), which can be bound to the lvalue reference parameter of foo()
.
Beware though: you are returning an lvalue reference to a temporary here. While using it inside foo()
is still safe (the temporary will be destroyed when the full-expression in which it is created is completely evaluated, so after foo()
returns), you would get Undefined Behavior if you stored that reference somewhere and dereferenced it later.
Therefore, instead of regarding this as a "solution to a compilation error", you should consider it a way to blindfold the compiler so you are free to sneak into big troubles.