Pre C ++ 11, at least the standard required the object to be in this context. In the end, semantics:
T const& t = f();
where f returns the value of T by value:
T tmp = f(); T const& t = tmp;
This requires a copy constructor.
In the case of std::auto_ptr problem you see is that the copy constructor is defined as a non-constant reference, which means you cannot copy the temporary one. Some compilers (for example, Microsoft) do not use this, which means that your code can work with them, but this is fundamentally illegal.
The real question is why are you using links here. You need a local variable anyway; only a link introduces an additional layer of indirection.
source share