Quick Q: Is this std::ref behaviour logical?
Quick A: Yes, std::ref can be reassigned (see example)
Recently on SO:
Is this std::ref behaviour logical?
A small modification to
f2provides the clue:template<class T> void f2(T arg) { arg.get() = xx; }This now does what you expect.
This has happened because
std::refreturns astd::reference_wrapper<>object. The assignment operator of which rebinds the wrapper. (see http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper/operator%3D)It does not make an assignment to the wrapped reference.
In the
f1case, all is working as you expected because astd::reference_wrapper<T>provides a conversion operator toT&, which will bind to the implicit right hand side of ints implicitoperator+.

Have you registered for CppCon 2016 in September? Don’t delay –
Have you registered for CppCon 2016 in September? Don’t delay –