auto specifier (C++11 起)
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
指定被声明的变量的类型将被自动推导出它的初始化。对于函数,指定的返回类型是一个后返回类型.
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 语法
| auto variable initializer (C++11 起) | |||||||||
| auto function -> return type (C++11 起) | |||||||||
[编辑] 解释
1)声明变量时,在块的范围,在命名空间范围内,在初始化语句的for循环等,被忽略的变量的类型和关键字auto可以用来代替.
Original:
When declaring variables in block scope, in namespace scope, in init statements of for loops, etc, the type of the variable may be omitted and the keyword auto may be used instead.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
的类型初始值设定项已被确定后,编译器决定,将取代关键字auto,如果使用的是从一个函数调用模板参数推导规则的类型。关键字auto可能会伴随着修改,如const或&,这将参与的类型推演。例如,给定const auto& i = expr;,类型
2) i是完全相同的参数的类型,在一个假想的模板utemplate<class U> void f(const U& u)如果函数调用f(expr)编译.Original:
Once the type of the initializer has been determined, the compiler determines the type that will replace the keyword auto as if using the rules for template argument deduction from a function call. The keyword auto may be accompanied by modifies, 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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
在函数声明,关键字auto不执行自动检测的类型。它只是作为后返回类型语法的一部分.
Original:
In a 函数声明, the keyword auto does not perform automatic type detection. It only serves as a part of the trailing return type syntax.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 注释
直到C + +11,auto的语义的存储时间说明.
Original:
Until C++11, auto had the semantic of a 存储时间说明.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 为例
#include <iostream> #include <cmath> #include <typeinfo> template<class T, class U> auto add(T t, U u) -> decltype(t + u) // the return type of add 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 int c; //compile-time error auto d = {1, 2}; std::cout << "type of d: " << typeid(d).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'; }
Output:
type of a: int type of b: double type of d: std::initializer_list<int> my_lambda: 8 type of my_fun: double (*)(double) my_fun: 0.14112

