For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer. For functions, specifies that the return type is a trailing return type or will be deduced from its return statements (since C++14). for non-type template parameters, specifies that the type will be deduced from the argument (since C++17).
auto variable initializer | (1) | (since C++11) |
auto function -> return type | (2) | (since C++11) |
auto function | (3) | (since C++14) |
decltype(auto) variable initializer | (4) | (since C++14) |
decltype(auto) function | (5) | (since C++14) |
auto :: | (6) | (concepts TS) |
cv(optional) auto ref(optional) parameter | (7) | (since C++14) |
template < auto Parameter > | (8) | (since C++17) |
cv(optional) auto ref(optional) [ identifier-list ] initializer ; | (9) | (since C++17) |
auto may be used as the type specifier.auto using the rules for template argument deduction from a function call (see template argument deduction#Other contexts for details). The keyword auto may be accompanied by modifiers, such as const or &, which will participate in the type deduction. For example, given const auto& i = expr;, the type of i is exactly the type of the argument u in an imaginary template template<class U> void f(const U& u) if the function call f(expr) was compiled. Therefore, auto&& may be deduced either as an lvalue reference or rvalue reference according to the initializer, which is used in range-based for loop.auto is used to declare multiple variables, the deduced types must match. For example, the declaration auto i = 0, d = 0.0; is ill-formed, while the declaration auto i = 0, *p = &i; is well-formed and the auto is deduced as int.auto does not perform automatic type detection. It only serves as a part of the syntax.auto indicates that the return type will be deduced from the operand of its return statement using the rules for template argument deduction.decltype(auto), the keyword auto is replaced with the expression (or expression list) of its initializer, and the actual type is deduced using the rules for decltype.decltype(auto), the keyword auto is replaced with the operand of its return statement, and the actual return type is deduced using the rules for decltype.auto:: is a placeholder that is replaced by a class or enumeration type following the rules for constrained type placeholder deduction.Until C++11, auto had the semantic of a storage duration specifier.
Mixing auto variables and functions in one declaration, as in auto f() -> int, i = 0; is not allowed.
The example showing output using one of the implementations where typeinfo::name prints full type names; filter through c++filt -t if using gcc or similar.
#include <iostream>
#include <cmath>
#include <typeinfo>
template<class T, class U>
auto add(T t, U u) -> decltype(t + u) // the return type is the type of operator+(T, U)
{
return t + u;
}
auto get_fun(int arg) -> double (*)(double) // same as: double (*get_fun(int))(double)
{
switch (arg)
{
case 1: return std::fabs;
case 2: return std::sin;
default: return std::cos;
}
}
int main()
{
auto a = 1 + 2;
std::cout << "type of a: " << typeid(a).name() << '\n';
auto b = add(1, 1.2);
std::cout << "type of b: " << typeid(b).name() << '\n';
auto c = {1, 2};
std::cout << "type of c: " << typeid(c).name() << '\n';
auto my_lambda = [](int x) { return x + 3; };
std::cout << "my_lambda: " << my_lambda(5) << '\n';
auto my_fun = get_fun(2);
std::cout << "type of my_fun: " << typeid(my_fun).name() << '\n';
std::cout << "my_fun: " << my_fun(3) << '\n';
// auto int x; // error as of C++11: "auto" is no longer a storage-class specifier
}Possible output:
type of a: int type of b: double type of c: std::initializer_list<int> my_lambda: 8 type of my_fun: double (*)(double) my_fun: 0.14112
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/language/auto