On 12-Jun-17 10:28 PM, Vir Campestris wrote:
> Why is it that when I call a function that returns a reference, and
> assign the result to an auto, it copies the value rather than declaring
> the auto as a reference?
I think Gareth Owen, in his reply, gave a good/likely possibility for
the rationale, namely the principle of least surprise.
But re the technical, you can view `auto` as `decltype` + `std::decay`.
Here are `std::decay<T>` rules quoted from
cppreference.com:
• If T names the type "array of U" or "reference to array of U", the
member typedef type is U*.
• Otherwise, if T is a function type F or a reference thereto, the
member typedef type is std::add_pointer<F>::type.
• Otherwise, the member typedef type is
std::remove_cv<std::remove_reference<T>::type>::type.
So, array-ness, function-ness, const-ness, volatile-ness and
reference-ness are removed.
> e.g.
>
> std::vector<char> v;
> ...
> auto c = v[0];
> auto& r = v[0]
>
> c is a char; to get a reference I must explicitly say so.
My main `auto` annoyance is that instead of
const ptr_<auto> p = foo();
… I must write something like
const auto p = pointer_result( foo() );
… to explicitly indicate the expected pointer-ness of the foo() result,
or else I'd have to use the operator notation; i.e., that `auto` is by
far not as powerful as template function argument deduction. :(
Cheers!,
- Alf