In C and before C++11, auto
is used as a counterpart of static
.
In practice it was never used, so the C++ comittee recycled the word for automatically deducing the type of a new object.
Contrary to dynamic languages like python and javascript, type deduction is done at compile time.
auto
is especially useful for objects with a specific interface and factory functions, like the iterators
#include <vector>
std::vector<int> vec = {1, 2, 3};
auto it = vec.begin();
and especially in templates
template <class C>
void print(const C& c){
for(typename C::const_iterator it = c.begin(); it != c.end(); ++it){
std::cout << *it << '\n';
}
}
vs
template <class C>
void print(const C& c){
for(auto it = c.begin(); it != c.end(); ++it){
std::cout << *it << '\n';
}
}
auto decays:
-
raw arrays convert to pointers
-
function converts to function pointers
-
top-level references are removed
-
top-level const/volatile qualifiers are removed
Those are the same rules that applies when passing or returning arguments by value.
int i = 42;
const int& j = i; // has type const int& and refers to i
auto v = r; // same as int v = r
const int arr[4] = {0,1,2,3};
auto a = arr; // has type const int*
auto str = "Hello World!";
// str has type const char*, while "..." is of type const char[13]
int& foo();
auto f = foo; // f has type int&(*)()
auto x = foo(); // has type int