Automatic Type Deduction: auto

In this lesson, we will discuss the automatic type deduction using auto.

The Facts of auto

Automatic type deduction with auto is extremely convenient. Firstly, we save unnecessary typing, in particular with challenging template expressions. Secondly, the compiler does not make human errors.

The compiler automatically deduces the type from the initializer:

auto myDoub = 3.14;

Key Features

  • The techniques for automatic function template argument deductions are used.
  • It is very helpful in complicated template expressions.
  • It empowers us to work with unknown types.
  • It must be used with care in combination with initializer lists.

The following code compares the definition of explicit and deduced types:

Get hands-on with 1000+ tech skills courses.