Auto
auto is a language feature that directs the compiler to deduce an object's type from its initializer.
Learn C++ by Example
Auto type deduction is the C++ language feature that lets the compiler infer a variable's type from its initializer.
- How it is used
- Uses type deduction to simplify variables, range-based loops, container code, generic addition, and selected function declarations.
- How deep it goes
- Explains deduction through expressions, containers, iterators, templates, and return types, while deferring fuller lambda and closure treatment.
- Examples
- Demonstrates arithmetic promotion, std::plus, vector iteration, Pascal's triangle, nested-vector returns, and generic lambdas.
- What you can do after
- Apply auto to simplify container programs, generic functions, range loops, and deduced function returns.
Modern C, Third Edition
auto is a C23 type-inference feature that allows an object's type to be inferred from its initializer.
- How it is used
- Supports type-generic declarations whose types must follow initializer expressions, especially inside macros and library-result handling.
- How deep it goes
- Explains initializer-based inference through SWAP and div examples, but does not enumerate the complete declaration constraint set.
- Examples
- Uses inferred arithmetic variables, a generic SWAP macro, and a div result exposing quotient and remainder members.
- What you can do after
- Apply it to maintain type consistency in generic declarations and to capture structure-valued results without spelling their types.
100 C++ Mistakes and How to Avoid Them
auto is a C++ declaration specifier that asks the compiler to deduce a variable's type from its initializer.
- How it is used
- Simplifies variable and iterator declarations by letting the compiler infer types from initializers.
- How deep it goes
- Explains deduction in loops and iterator declarations, then warns that unsigned deduction can break countdown loops.
- Examples
- Contrasts an index loop and std::find iterator using auto with an unsafe reverse loop.
- What you can do after
- Diagnose when deduction helps and when an explicitly signed loop variable avoids unsigned underflow.
Read how 100 C++ Mistakes and How to Avoid Them explains auto →
What to know first
- iteratorAn iterator is an object or abstraction that provides successive values from a collection or other data source through a traversal interface.
- range-based for loopA range-based for loop is an iteration statement that traverses the elements of a range or container without explicit index or iterator management.
Related concepts
- expression searchuses · Expression search is a type-directed development technique that searches for an expression satisfying the type of an incomplete program term.
- C23contrasts with · C23 is the ISO standard revision of the C programming language published in 2024.
- type inferenceimplements · Type inference is the automatic deduction of program types from expressions, values, declarations, usage, or contextual constraints without requiring every type to be specified explicitly.
- compileruses · A compiler is software that translates source code into another representation, such as machine code, bytecode, or an intermediate form.
- trailing return typeuses · A trailing return type is C++ syntax that places a function's return type after its parameter list using an arrow.
- typeof operatoralternative to · A typeof operator obtains type information about an operand or expression, yielding a type or a string describing its type depending on the language.
Questions about auto
What is auto?
auto is a language feature that directs the compiler to deduce an object's type from its initializer.
Which Manning books explain auto?
Learn C++ by Example, Modern C, Third Edition and 100 C++ Mistakes and How to Avoid Them.
What should I know before learning auto?
Manning books that teach auto usually introduce iterator and range-based for loop first.
What is the difference between auto and C23?
auto is a language feature that directs the compiler to deduce an object's type from its initializer. C23 is the ISO standard revision of the C programming language published in 2024.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


