c++汽车关键字。为什么它是魔法?

时间:2022-02-11 16:13:57

From all the material I used to learn C++, auto has always been a weird storage duration specifier that didn't serve any purpose. But just recently, I encountered code that used it as a type name in and of itself. Out of curiosity I tried it, and it assumes the type of whatever I happen to assign to it!

从我学习c++的所有材料中,auto一直以来都是一个奇怪的存储时间指示器,没有任何用途。但就在最近,我遇到了一些代码,这些代码将它本身用作类型名。出于好奇,我尝试了一下,它假定了我所赋予它的类型!

Suddenly STL iterators and, well, anything at all that uses templates is 10 fold easier to write. It feels like I'm using a 'fun' language like Python.

突然间,STL迭代器和所有使用模板的东西都变得更容易编写了。感觉上我在使用一种像Python这样的“有趣”语言。

Where has this keyword been my whole life? Will you dash my dreams by saying it's exclusive to visual studio or not portable?

这个关键词在哪里是我的一生?你会说它是visual studio独有的,还是不能移植的?

5 个解决方案

#1


90  

auto was a keyword that C++ "inherited" from C that had been there nearly forever, but virtually never used because there were only two possible conditions: either it wasn't allowed, or else it was assumed by default.

auto是c++从C中“继承”的一个关键字,它几乎一直存在,但实际上从未使用过,因为只有两个可能的条件:要么不允许使用,要么默认使用。

The use of auto to mean a deduced type is new with C++11 (formerly called C++0x). Since the C++11 standard was published extremely recently, there may still be some compilers that haven't been updated to understand it (yet).

使用auto表示推断的类型是c++ 11的新用法(以前称为c++ 0x)。由于最近发布了c++ 11标准,可能还有一些编译器还没有更新以了解它(然而)。

At the same time, auto x works pretty much the same way as template type deduction works for function templates. Consider a function template like this:

与此同时,auto x的工作方式与模板类型演绎的工作方式基本相同。考虑这样的函数模板:

template<class T>
int whatever(T t) { 
    // point A
};

At point A, a type has been assigned to T based on the value passed for the parameter to whatever. When you do auto x = some_expression;, essentially the same type deduction mechanism is used to determine the type for x from the type of some_expression that's used to initialize it.

在点A,根据为参数传递的值将类型分配给T。当您执行auto x = some_expression时,基本上使用相同的类型推断机制,从用于初始化x的some_expression的类型中确定x的类型。

This means that most of the type deduction mechanics a compiler needs to implement auto are already present and used for templates on any compiler that even sort of attempts to implement C++98/03. As such, even for those compilers that don't support C++11 yet, adding support for auto will probably be fairly quick and easy.

这意味着编译器需要实现自动的大多数类型推断机制已经存在,并用于任何试图实现c++ 98/03的编译器上的模板。因此,即使对于那些还不支持c++ 11的编译器来说,添加对auto的支持也可能是相当快速和容易的。

When this answer was originally written (in 2011, before the ink was dry on the C++ 11 standard) auto was already quite portable. Nowadays, it's thoroughly portable among all the mainstream compilers. The only obvious reasons to avoid it would be if you need to write code that's compatible with a C compiler, or you have a specific need to target some niche compiler that you know doesn't support it (e.g., a few people still write code for MS-DOS using compilers from Borland, Watcom, etc., that haven't seen significant upgrades in decades).

当这个答案最初被写出来的时候(在2011年,c++ 11标准的墨水还没有干的时候),auto就已经相当便携了。现在,它在所有主流编译器中都是完全可移植的。唯一显而易见的原因,以避免它如果您需要编写的代码兼容的C编译器,或你有一个特定的目标一些利基编译器需要知道它不支持(例如,一些人仍然为ms - dos使用编译器从Borland编写代码,Watcom,等等,几十年没见过重大升级)。

#2


19  

It's just taking a generally useless keyword and giving it a new, better functionality. It's standard in C++11, and most C++ compilers with even some C++11 support will support it.

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

#3


10  

This functionality hasn't been there your whole life. It's been supported in Visual Studio since the 2010 version. It's a new C++11 feature, so it's not exclusive to Visual Studio and is/will be portable. Most compilers support it already.

这个功能在你的一生中都没有出现过。自2010年版本以来,Visual Studio一直支持它。这是一个新的c++ 11特性,因此它不是Visual Studio独有的,并且是/将是可移植的。大多数编译器已经支持它了。

#4


5  

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).

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

Syntax

语法

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)

Explanation

解释

1) 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.

1)在块范围、名称空间范围、for循环的初始化语句等声明变量时,可以使用关键词auto作为类型说明符。一旦确定了初始化器的类型,编译器将使用函数调用中的模板参数演绎规则确定替换关键字auto的类型(详细信息请参见模板参数演绎#Other上下文中)。关键字auto可能伴随着修饰符,比如const或&,它们将参与类型演绎。例如,给定const auto& i = expr;,如果函数调用f(expr)被编译,那么i的类型就是假想模板 void f(const u&u)中的参数u的类型。因此,可以根据初始化器将auto&&作为lvalue引用或rvalue引用,在基于范围的for循环中使用。如果使用auto声明多个变量,则推断的类型必须匹配。例如,自动i = 0, d = 0;为病态,而申报自动i = 0, *p = &i;是结构良好的,汽车被推断为int。

2) 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.

2)在使用尾返回类型语法的函数声明中,关键字auto不执行自动类型检测。它只作为语法的一部分。

3) 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.

3)在不使用尾返回类型语法的函数声明中,关键字auto表示返回类型将使用模板参数演绎规则从返回语句的操作数推导出来。

4) 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.

4)如果变量声明的类型为decltype(auto),关键字auto就会被替换为其初始化器的表达式(或表达式列表),实际的类型就会使用decltype规则来推断。

5) 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.

5)如果函数的返回类型被声明为decltype(auto),那么关键字auto就会被替换为返回语句的操作数,实际的返回类型就会使用decltype规则来推断。

6) 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.

6)自动::是一个占位符,按照约束类型占位符演绎规则由类或枚举类型替换。

7) A parameter declaration in a lambda expression. (since C++14) A function parameter declaration. (concepts TS)

7) lambda表达式中的参数声明。(因为c++ 14)函数参数声明。(TS)的概念

Notes 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.

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

For more info : http://en.cppreference.com/w/cpp/language/auto

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

#5


3  

It's not going anywhere ... it's a new standard C++ feature in the implementation of C++11. That being said, while it's a wonderful tool for simplifying object declarations as well as cleaning up the syntax for certain call-paradigms (i.e., range-based for-loops), don't over-use/abuse it :-)

它不会去任何地方……它是c++实现c++的一个新标准特性。话虽如此,但它是简化对象声明以及清理特定调用-范式语法的一个很好的工具。),不要过度使用/滥用它:-)

#1


90  

auto was a keyword that C++ "inherited" from C that had been there nearly forever, but virtually never used because there were only two possible conditions: either it wasn't allowed, or else it was assumed by default.

auto是c++从C中“继承”的一个关键字,它几乎一直存在,但实际上从未使用过,因为只有两个可能的条件:要么不允许使用,要么默认使用。

The use of auto to mean a deduced type is new with C++11 (formerly called C++0x). Since the C++11 standard was published extremely recently, there may still be some compilers that haven't been updated to understand it (yet).

使用auto表示推断的类型是c++ 11的新用法(以前称为c++ 0x)。由于最近发布了c++ 11标准,可能还有一些编译器还没有更新以了解它(然而)。

At the same time, auto x works pretty much the same way as template type deduction works for function templates. Consider a function template like this:

与此同时,auto x的工作方式与模板类型演绎的工作方式基本相同。考虑这样的函数模板:

template<class T>
int whatever(T t) { 
    // point A
};

At point A, a type has been assigned to T based on the value passed for the parameter to whatever. When you do auto x = some_expression;, essentially the same type deduction mechanism is used to determine the type for x from the type of some_expression that's used to initialize it.

在点A,根据为参数传递的值将类型分配给T。当您执行auto x = some_expression时,基本上使用相同的类型推断机制,从用于初始化x的some_expression的类型中确定x的类型。

This means that most of the type deduction mechanics a compiler needs to implement auto are already present and used for templates on any compiler that even sort of attempts to implement C++98/03. As such, even for those compilers that don't support C++11 yet, adding support for auto will probably be fairly quick and easy.

这意味着编译器需要实现自动的大多数类型推断机制已经存在,并用于任何试图实现c++ 98/03的编译器上的模板。因此,即使对于那些还不支持c++ 11的编译器来说,添加对auto的支持也可能是相当快速和容易的。

When this answer was originally written (in 2011, before the ink was dry on the C++ 11 standard) auto was already quite portable. Nowadays, it's thoroughly portable among all the mainstream compilers. The only obvious reasons to avoid it would be if you need to write code that's compatible with a C compiler, or you have a specific need to target some niche compiler that you know doesn't support it (e.g., a few people still write code for MS-DOS using compilers from Borland, Watcom, etc., that haven't seen significant upgrades in decades).

当这个答案最初被写出来的时候(在2011年,c++ 11标准的墨水还没有干的时候),auto就已经相当便携了。现在,它在所有主流编译器中都是完全可移植的。唯一显而易见的原因,以避免它如果您需要编写的代码兼容的C编译器,或你有一个特定的目标一些利基编译器需要知道它不支持(例如,一些人仍然为ms - dos使用编译器从Borland编写代码,Watcom,等等,几十年没见过重大升级)。

#2


19  

It's just taking a generally useless keyword and giving it a new, better functionality. It's standard in C++11, and most C++ compilers with even some C++11 support will support it.

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

#3


10  

This functionality hasn't been there your whole life. It's been supported in Visual Studio since the 2010 version. It's a new C++11 feature, so it's not exclusive to Visual Studio and is/will be portable. Most compilers support it already.

这个功能在你的一生中都没有出现过。自2010年版本以来,Visual Studio一直支持它。这是一个新的c++ 11特性,因此它不是Visual Studio独有的,并且是/将是可移植的。大多数编译器已经支持它了。

#4


5  

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).

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

Syntax

语法

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)

Explanation

解释

1) 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.

1)在块范围、名称空间范围、for循环的初始化语句等声明变量时,可以使用关键词auto作为类型说明符。一旦确定了初始化器的类型,编译器将使用函数调用中的模板参数演绎规则确定替换关键字auto的类型(详细信息请参见模板参数演绎#Other上下文中)。关键字auto可能伴随着修饰符,比如const或&,它们将参与类型演绎。例如,给定const auto& i = expr;,如果函数调用f(expr)被编译,那么i的类型就是假想模板 void f(const u&u)中的参数u的类型。因此,可以根据初始化器将auto&&作为lvalue引用或rvalue引用,在基于范围的for循环中使用。如果使用auto声明多个变量,则推断的类型必须匹配。例如,自动i = 0, d = 0;为病态,而申报自动i = 0, *p = &i;是结构良好的,汽车被推断为int。

2) 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.

2)在使用尾返回类型语法的函数声明中,关键字auto不执行自动类型检测。它只作为语法的一部分。

3) 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.

3)在不使用尾返回类型语法的函数声明中,关键字auto表示返回类型将使用模板参数演绎规则从返回语句的操作数推导出来。

4) 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.

4)如果变量声明的类型为decltype(auto),关键字auto就会被替换为其初始化器的表达式(或表达式列表),实际的类型就会使用decltype规则来推断。

5) 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.

5)如果函数的返回类型被声明为decltype(auto),那么关键字auto就会被替换为返回语句的操作数,实际的返回类型就会使用decltype规则来推断。

6) 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.

6)自动::是一个占位符,按照约束类型占位符演绎规则由类或枚举类型替换。

7) A parameter declaration in a lambda expression. (since C++14) A function parameter declaration. (concepts TS)

7) lambda表达式中的参数声明。(因为c++ 14)函数参数声明。(TS)的概念

Notes 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.

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

For more info : http://en.cppreference.com/w/cpp/language/auto

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

#5


3  

It's not going anywhere ... it's a new standard C++ feature in the implementation of C++11. That being said, while it's a wonderful tool for simplifying object declarations as well as cleaning up the syntax for certain call-paradigms (i.e., range-based for-loops), don't over-use/abuse it :-)

它不会去任何地方……它是c++实现c++的一个新标准特性。话虽如此,但它是简化对象声明以及清理特定调用-范式语法的一个很好的工具。),不要过度使用/滥用它:-)