Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The ref- and cv-stripping property of `auto`.

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;
like image 867
Ralph Tandetzky Avatar asked Jun 24 '13 10:06

Ralph Tandetzky


1 Answers

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;.

like image 127
R. Martinho Fernandes Avatar answered Nov 01 '22 23:11

R. Martinho Fernandes