C ++ auto keyword. Why is this magic?

Of all the material I used to learn C ++, auto always a weird storage duration specifier that served no purpose. But more recently, I came across a code that used it as a type name in itself. Out of curiosity, I tried this, and he suggests the type of what I assign to him!

Suddenly, STL iterators and, well, anything that uses templates are 10 times easier to write. Looks like I'm using a โ€œfunnyโ€ language like Python.

Where has this keyword been my whole life? Can you plunge into my dreams by saying this exclusively for a visual studio or not portable?

+120
c ++ types c ++ 11 auto
Sep 28 2018-11-11T00:
source share
5 answers

auto was a keyword that C ++ โ€œinheritedโ€ from C, which almost always existed there, but was almost never used, because there were only two possible conditions: either it was forbidden, or it was assumed by default.

Using auto to denote a deduced type was first introduced in C ++ 11.

At the same time, auto x = initializer deduces the type x from the type initializer in the same way that the deduction of template types works for function templates. Consider the function template as follows:

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

At point A, the type was assigned to T based on the value passed to the parameter in whatever . When you do auto x = initializer; the same hold type is used to determine the type for x from the initializer type that was used to initialize it.

This means that most of the type inference mechanisms needed by the compiler to implement auto already present and used for templates in any compiler that even tried to implement C ++ 98/03. Thus, adding auto support was apparently quite simple for almost all compiler commands - it was added quite quickly, and it seems that several errors were also associated with this.

When this answer was originally written (in 2011, before the ink was dried according to the C ++ 11 standard), auto already quite portable. Currently, it is fully portable among all major compilers. The only obvious reasons why you should avoid this may be that you need to write code compatible with the C compiler, or you have a special need to target some specialized compiler that you know doesn't support it (e.g. , a few people still write code for MS-DOS using Borland, Watcom, etc. compilers that have not been updated for decades). If you use a sufficiently current version of any of the main compilers, there is no reason to avoid this at all.

+119
Sep 28 2018-11-11T00: 00Z
source share

It simply uses the usually useless keyword and gives it new, better functionality. It is standard in C ++ 11, and most C ++ compilers with support for some C ++ 11 support it.

+19
Sep 28 2018-11-11T00:
source share

This functionality has not been there all my life. It has been supported in Visual Studio since 2010. This is a new C ++ 11 feature, therefore it is not exclusive to Visual Studio and is / will be portable. Most compilers already support it.

+10
Sep 28 2018-11-11T00:
source share

For variables, indicates that the type of the declared variable will be automatically inferred from its initializer. For functions, indicates that the return type is the final return type or will be inferred from its return statements (starting with 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 a block scope, in a namespace scope, in for loop initialization statements, etc. The auto keyword can be used as a type specifier. Once the initializer type is determined, the compiler determines the type that will replace the auto keyword, using the rules to derive the template argument from the function call (see Details in the section Deriving the template argument # Other contexts). The auto keyword may be followed by modifiers such as const or &, which will participate in type inference. For example, if const auto& = expr; type i exactly matches the type of argument u in an imaginary template template<class U> void f(const U& u) if the function call f(expr) was compiled. Therefore, auto && can be displayed either as an lvalue reference or as an rvalue reference according to the initializer used in the range-based for loop. If auto is used to declare multiple variables, the inferred types must match. For example, auto = 0, d = 0.0; declaration auto = 0, d = 0.0; malformed while auto = 0, *p = &i; declaration auto = 0, *p = &i; correctly formed, and the car is displayed as int.

2) In a function declaration that uses the final syntax of the return type, the auto keyword does not automatically detect the type. This is only part of the syntax.

3) In a function declaration that does not use the final syntax of the return type, the auto keyword indicates that the type of the return value will be inferred from the operand of the return statement, using the rules to output the template argument.

4) If the declared type of the variable is decltype (auto), the auto keyword is replaced by the expression (or list of expressions) of its initializer, and the actual type is determined using the rules for decltype.

5) If the return type of the function is declared decltype (auto), the auto keyword is replaced with the operand of its return statement, and the actual type of the return value is determined using the rules for decltype.

6) The nested name specifier in the form auto :: is a placeholder that is replaced with a class or enumeration type in accordance with the rules for deriving a limited type placeholder.

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

Notes Prior to C ++ 11, auto had storage duration specifier semantics. Mixing automatic variables and functions in one declaration, as in auto f() โ†’ int, = 0; not allowed.

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

+8
Jun 30 '16 at 2:04 on
source share

This is not going anywhere ... this is a new standard C ++ function in the implementation of C ++ 11. At the same time, although this is a wonderful tool for simplifying object declarations, as well as for clearing the syntax of certain calling paradigms (i.e., for ranges based on cycles), do not use it / abuse :-)

+3
Sep 28 2018-11-11T00:
source share



All Articles