从我用来学习c++的所有材料来看,auto一直是一个奇怪的存储持续时间指示符,没有任何用处。但就在最近,我遇到了一些代码,它们将它本身用作类型名。出于好奇,我尝试了一下,它假设我分配给它的任何类型!

突然间,STL迭代器,以及任何使用模板的东西都变得容易了10倍。感觉我在使用一种“有趣”的语言,比如Python。

这个关键字在我的生活中哪里呢?你会说这是visual studio独有的或者不能携带的,这会让我的梦想破灭吗?


当前回答

对于变量,指定要声明的变量的类型将自动从其初始化式推导出来。对于函数,指定返回类型是尾随返回类型或将从其返回语句中推导出(自c++ 14起)。

语法

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)

解释

When declaring variables in block scope, in namespace scope, in initialization statements of for loops, etc., the keyword auto may be used as the type specifier. Once the type of the initializer has been determined, the compiler determines the type that will replace the keyword 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. If 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. In a function declaration that uses the trailing return type syntax, the keyword auto does not perform automatic type detection. It only serves as a part of the syntax. In a function declaration that does not use the trailing return type syntax, the keyword auto indicates that the return type will be deduced from the operand of its return statement using the rules for template argument deduction. If the declared type of the variable is 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. If the return type of the function is declared 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. A nested-name-specifier of the form auto:: is a placeholder that is replaced by a class or enumeration type following the rules for constrained type placeholder deduction. A parameter declaration in a lambda expression. (since C++14) A function parameter declaration. (concepts TS)

笔记

在c++ 11之前,auto具有存储持续时间说明符的语义。 将auto变量和函数混合在一个声明中,如auto f() -> int, i = 0;是不允许的。

欲了解更多信息:http://en.cppreference.com/w/cpp/language/auto

其他回答

对于变量,指定要声明的变量的类型将自动从其初始化式推导出来。对于函数,指定返回类型是尾随返回类型或将从其返回语句中推导出(自c++ 14起)。

语法

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)

解释

When declaring variables in block scope, in namespace scope, in initialization statements of for loops, etc., the keyword auto may be used as the type specifier. Once the type of the initializer has been determined, the compiler determines the type that will replace the keyword 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. If 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. In a function declaration that uses the trailing return type syntax, the keyword auto does not perform automatic type detection. It only serves as a part of the syntax. In a function declaration that does not use the trailing return type syntax, the keyword auto indicates that the return type will be deduced from the operand of its return statement using the rules for template argument deduction. If the declared type of the variable is 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. If the return type of the function is declared 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. A nested-name-specifier of the form auto:: is a placeholder that is replaced by a class or enumeration type following the rules for constrained type placeholder deduction. A parameter declaration in a lambda expression. (since C++14) A function parameter declaration. (concepts TS)

笔记

在c++ 11之前,auto具有存储持续时间说明符的语义。 将auto变量和函数混合在一个声明中,如auto f() -> int, i = 0;是不允许的。

欲了解更多信息:http://en.cppreference.com/w/cpp/language/auto

这种功能并不是你一生中就有的。自2010年版本以来,Visual Studio一直支持它。它是c++ 11的一个新特性,所以它不是Visual Studio独有的,并且是可移植的。大多数编译器已经支持它了。

它的神奇之处在于它能够减少为传递到特定函数的每个变量类型编写代码。考虑Python中类似的print()函数。

#include <iostream>
#include <string>
#include <array>

using namespace std;

void print(auto arg) {
     cout<<arg<<" ";
}

int main()
{
  string f = "String";//tok assigned
  int x = 998;
  double a = 4.785;
  string b = "C++ Auto !";
//In an opt-code ASCII token stream would be iterated from tok's as:
  print(a);
  print(b);
  print(x);
  print(f);
}

auto关键字是c++中一个重要且常用的关键字。在初始化变量时,auto关键字用于类型推断(也称为类型推断)。

关于auto关键字有3条不同的规则。

第一条规则

Auto x = expr;---->没有指针或引用,只有变量名。在这种情况下,const和reference将被忽略。

int  y = 10;
int& r = y;
auto x = r; // The type of variable x is int. (Reference Ignored)

const int y = 10;
auto x = y; // The type of variable x is int. (Const Ignored)

int y = 10;
const int& r = y;
auto x = r; // The type of variable x is int. (Both const and reference Ignored)

const int a[10] = {};
auto x = a; //  x is const int *. (Array to pointer conversion)

Note : When the name defined by auto is given a value with the name of a function,
       the type inference will be done as a function pointer.

第二条规则

Auto& y = expr;或auto* y = expr;----> auto关键字后的引用或指针。

警告:const在此规则中不会被忽略!!.

int y = 10;
auto& x = y; // The type of variable x is int&.

警告:在此规则中,数组到指针的转换(数组衰减)不会发生!!

auto& x = "hello"; // The type of variable x is  const char [6].

static int x = 10;
auto y = x; // The variable y is not static.Because the static keyword is not a type. specifier 
            // The type of variable x is int.

第三条规则

Auto&& z = expr;---->不是右值引用。

警告:如果存在类型推断问题,并且使用了&&标记,则名称 像这样引入的被称为“转发引用”(也称为通用引用)。

auto&& r1 = x; // The type of variable r1 is int&.Because x is Lvalue expression. 

auto&& r2 = x+y; // The type of variable r2 is int&&.Because x+y is PRvalue expression. 

它只是采用一个通常无用的关键字,并赋予它一个新的、更好的功能。它是c++ 11中的标准,大多数c++编译器甚至有一些c++ 11支持都会支持它。