Вопрос

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top