Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The auto specifier

Reference for C++ auto: deducing variable types, auto& and const auto&, structured bindings, trailing return types, generic lambdas, decltype(auto) and abbreviated templates.

auto asks the compiler to work out a type from context. For a variable it deduces the type from the initializer: auto n = 5; makes n an int. For a function return type (C++14) it deduces from the return statements, and for parameters (C++14 lambdas, C++20 functions) it makes the function a template.

Deduction removes noise (std::map<std::string, std::vector<int>>::const_iterator) and prevents some mismatches, but it can also hide a type that matters. The key rule: auto follows the same rules as template argument deduction, so it drops references and top-level const unless you add them back.

C++
auto x = 3.5;            // double
const auto& r = x;       // const double&
auto [a, b] = std::pair{1, 'z'};   // structured bindings

Try it

C++ What auto deduces

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ auto return types, decltype(auto) and generic functions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
auto name = expr;                  // deduced variable
auto& name = expr;                 // lvalue reference
const auto& name = expr;           // binds to anything, extends temporary lifetime
auto&& name = expr;                // forwarding reference
auto f(params) { return expr; }    // deduced return type (C++14)
auto f(params) -> Type;            // trailing return type
decltype(auto) name = expr;        // keeps references and const (C++14)
void g(auto param);                // abbreviated function template (C++20)

Usage notes

  • Plain auto always copies. Use auto& or const auto& when you want a reference, especially in range-based for loops over large objects.
  • auto x = {1, 2, 3}; deduces std::initializer_list<int>, whereas auto x{1}; (C++17) deduces int. Be careful with braces.
  • auto with proxies can surprise you: auto b = vec_bool[0]; yields std::vector<bool>::reference, not bool.
  • Deduction from a string literal gives const char*, not std::string. Use "text"s or an explicit type.
  • A function with deduced return type must be defined before it is used, and all return statements must deduce the same type. Recursive calls need an earlier return.
  • decltype(auto) preserves the exact value category: return (x); returns a reference, return x; returns a copy.
  • Use auto when the type is obvious from the right-hand side (auto p = std::make_unique<Foo>(), auto it = v.begin()) or irrelevant; spell the type out when it documents intent, such as std::uint8_t.
  • auto is also a placeholder for constrained deduction: std::integral auto n = ...; (C++20, see concepts).
  • Added in C++11; return type and lambda deduction in C++14; auto in structured bindings C++17; abbreviated templates in C++20.