auto
auto asks the compiler to deduce a variable's type from its initialiser. auto n = 3; is int. auto is not a dynamic type: the type is fixed at compile time, just not spelled by you.
Detailed Explanation
Iterator types used to look like std::map<std::string, std::vector<int>>::const_iterator. Writing that twice in a function hid the work the function was doing. auto (C++11) lets you write auto it = ages.find(name); and keep the interesting part, the find.
Deduction follows the rules of template argument deduction for a parameter of the form you wrote. auto x = expr; is the same deduction as template<class T> void f(T); f(expr);. References and top-level const on the initialiser are dropped: const int n = 1; auto x = n; makes x an int, a copy. auto& r = n; preserves const and binds a const int&. auto&& is a forwarding reference and binds to both lvalues and rvalues.
That drop-const-and-ref rule is the one that surprises people. auto x = getName(); copies (or moves) the string. const auto& x = getName(); binds to the temporary and extends its life if getName() returned a temporary by value... actually: if getName() returns std::string by value, const auto& extends the temporary. If it returns const std::string& into an object that dies, you dangle.
auto is required in some places. A lambda's type has no name you can write; auto add = [](int a, int b) { return a + b; }; is the only local way to store it besides std::function. Structured bindings use auto (auto [k, v] = *it;). C++14 generic lambdas use auto in the parameter list. C++20 function parameters can be auto, which is an abbreviated function template.
C++14 added auto as a return type with deduction from return statements. All returns must agree. C++14 also added decltype(auto), which applies decltype rules instead of template rules: it keeps references. decltype(auto) f() { return x; } returns int& if x is an int&. Plain auto f() { return x; } returns int.
When not to use auto: when the type is the documentation. auto n = 0; hides nothing useful, and auto x = 0; versus auto x = 0.0; is a silent int/double choice. When a conversion is the point (std::size_t n = v.size();), spelling the type is clearer than auto. When the initialiser is a proxy (std::vector<bool>::operator[]), auto copies the proxy and you get a dangling or a value that does not write back.
auto does not mean "I do not know the type". It means "the type is obvious from the right-hand side, or unspeakable". Readers should still be able to say what x is after reading the initialiser.
Code Examples
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> ages{{"ada", 36}};
auto it = ages.find("ada"); // map<...>::iterator
if (it != ages.end()) {
std::cout << it->second << '\n'; // prints: 36
}
const int limit = 10;
auto copy = limit; // int, not const int
copy = 11;
const auto& view = limit; // const int&
std::cout << copy << ' ' << view << '\n'; // prints: 11 10
}#include <iostream>
#include <vector>
auto sum(const std::vector<int>& v) { // C++14 return deduction
int total = 0;
for (auto n : v) total += n; // auto n is int, a cheap copy
return total;
}
int main() {
std::cout << sum({1, 2, 3}) << '\n'; // prints: 6
}Common Gotchas
`auto` drops references
`auto x = v[0];` copies the element. `v[0]` on a `vector<bool>` yields a proxy; the proxy dies at the end of the statement and `x` may not be a real `bool`. Use `auto&` or avoid `vector<bool>`.
Integer literals are `int`
`auto n = 0;` is not `std::size_t`. Mixing `n` with `v.size()` reintroduces signed/unsigned warnings.
`auto` in a parameter makes a template
`void f(auto x)` is `template<class T> void f(T x)`. The function must live where templates live, usually a header.
`auto` variable without an initialiser
`auto x;` does not compile. Deduction needs an expression.