Advertisement
Beginner C++, Type Deduction & auto

What is the auto keyword?

Short answer

auto lets the compiler deduce a variable's type from its initializer, reducing verbosity and coupling to exact types. auto x = expr; gives x the type of expr (with top-level const and references stripped unless specified).

Save C++ mock interview
Advertisement
AModel answer
auto lets the compiler deduce a variable's type from its initializer, reducing verbosity and coupling to exact types. auto x = expr; gives x the type of expr (with top-level const and references stripped unless specified). It's especially useful for complex types like iterators and lambdas.
auto i = 42;                 // int
auto it = v.begin();         // iterator type deduced
auto f = [](int x){return x;};
Real-world example
Using auto for an iterator avoids spelling out std::vector<std::string>::const_iterator.
Likely follow-ups
  • Does auto keep references and const? | Where is auto most helpful?
Related topics