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.
auto x = 3.5; // double const auto& r = x; // const double& auto [a, b] = std::pair{1, 'z'}; // structured bindings
Try it
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.
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
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
autoalways copies. Useauto&orconst auto&when you want a reference, especially in range-basedforloops over large objects. auto x = {1, 2, 3};deducesstd::initializer_list<int>, whereasauto x{1};(C++17) deducesint. Be careful with braces.autowith proxies can surprise you:auto b = vec_bool[0];yieldsstd::vector<bool>::reference, notbool.- Deduction from a string literal gives
const char*, notstd::string. Use"text"sor an explicit type. - A function with deduced return type must be defined before it is used, and all
returnstatements must deduce the same type. Recursive calls need an earlierreturn. decltype(auto)preserves the exact value category:return (x);returns a reference,return x;returns a copy.- Use
autowhen 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 asstd::uint8_t. autois 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;
autoin structured bindings C++17; abbreviated templates in C++20.