I learned that declaring a variable using auto in this way
auto var = expr;
basically is like taking the type of expr
and stripping &/&&-references and all top-level constness and volatileness from it. Does this mean that the above line is exactly equivalent to the following?
std::remove_cv<std::remove_ref<decltype(expr)>::type>::type var = expr;
No, that is not true. auto var = expr;
is more like passing expr
by value.
int x[1];
auto y = x;
This makes y
a int*
.
Mostly auto x = expr;
behaves like template type deduction:
template <typename T>
void f(T);
int x[1];
f(x); // deduces T as int*
It is more like std::decay<decltype(expr)> var = expr;
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With