auto Keyword
intermediateThe auto keyword lets the compiler deduce the type automatically.
Basic Usage
When auto is Useful
auto with References
auto with Pointers
auto for Loop
Structured Bindings (C++17)
When NOT to Use auto
- When type is not obvious from context
- When you need a specific type
- In function signatures (use explicit types)
Common mistakes: auto drops qualifiers and references, so auto x = obj silently copies instead of binding (use auto& or const auto&); auto also deduces proxy types such as vector<bool>::reference rather than bool.
Key takeaway: auto deduces types from initializers — use it when the type is obvious from context, not for function signatures.
Try it: Use auto for a variable, for an iterator over a vector, and in a range-based for loop, then print each value.