C + + Auto Properties

Source: Internet
Author: User

Auto specifier(from c++11)C + + language declaration

For a variable, the specified type is automatically deduced from its initializer.

For a function, specify whether its return type is a trailing return type or will derive from its return statement (C++14).

For non-type template parameters, the specified type is deduced from the parameter. (from c++17)

Grammar
Auto variable initializer (1) (from C++11)
Auto function, return type (2) (from C++11)
Auto function (3) (from c++14)
Decltype(auto) variable initializer (4) (from c++14)
Decltype(auto) function (5) (from c++14)
Auto:: (6) (Concept TS)
CV(optional) Auto ref(optional) parameter (7) (from c++14)
template< auto Parameter > (8) (from c++17)
CV(optional) Auto ref(optional) [ identifier-list ] initializer ; (9) (from c++17)
explain1) Keyword Auto can be used as a type specifier when declaring variables in block scope, namespace scope, circular initialization statement, and so on .

As long as the type of the initializer is determined, the compiler replaces the keyword with the type determined by the template argument deduction rule from the function callAuto (see template argument derivation for details). KeywordsAuto can be combined with modifiers, such asConst OR&, they will participate in type deduction. For example, give aconst auto& i =  expr;&NBSP;,&NBSP; i the type of   is just a fictional template  template<class u > void f (const u< Span class= "Sy3" >& u u   Type, if function calls   f (Exprauto& &  can be deduced as an lvalue or rvalue reference based on the initializer, which is used for a range-based for loop.

auto  is used to declare multiple variables, the deduction type must match. For example, declare  auto i  = 0, D = 0.0 ;  is disease-type, and declares   auto i = 0, *p  = &i;  for good type, here   auto  is deduced into int. 2) In a function declaration that uses the trailing return type syntax, the keyword auto does not perform automatic type detection. It works only as part of the syntax. 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 template argument deduction rule through the operands of its return statement. 4) If the declared type of the variable is decltype(auto), the keyword Auto is replaced with an expression (or list of expressions) of its initializer, and the actual type is deduced by the Decltype rule. 5) If the declaration type of the function return value is decltype(auto), the keyword Auto is replaced with the operand of its return statement, and its actual return type is deduced by the Decltype rule. 6) Owning form Auto:: The nested name specifier is a placeholder that is replaced by a class or enumeration type that follows the constraint type placeholder deduction rule. 7) a parameter declaration in a lambda expression. (c++14) function parameter declaration. (concept TS) 8) If the template parameter is declared auto , its type is deduced from the corresponding usage parameter. 9) Structured binding declarationNote

Before c++11, Auto has the semantics of the storage period specifier.

Mixing variables and functions in a declaration is not allowed auto , such as auto F() -> int, i = 0;.

ExampleRun this Code
#include <iostream>#include <utility>Template<Class T,Class U>Auto Add(T T, u u){Return T+ U;}The return type is a type of operator+ (T, U)In the case where the function it is calling returns a referenceThe perfect forwarding of a function call must be Decltype (auto)Template<Class F,Class ...Args>decltype(Auto) Perfectforward(F fun, Args&&amp, .....Args){return fun(std::forward <args>(args)...);}Template<Auto N>C++17 Auto Parameter DeclarationAuto F()->std::pair <decltype(n), Decltype(n)>Auto cannot derive from curly brace initializer list{Return{N, n};}int main(){Auto A=1+2;The type of a is intAuto B= Add(1,1.2);The type of B is double Static_assert(std::is_same_v <decltype(A),Int>); Static_assert(std::is_same_v <decltype(b),Double>); Decltype(Auto) C1= A;The type of C1 is int, which holds a copy of a decltype(Auto) C2=(A);The type of C2 is int&, an alias for astd::cout  <<"A, before modification through C2 ="<< A<<‘\ n ';++c2;std::cout  <<"A, after modification through C2 ="<< A<<‘\ n ';Auto[V, W]= f<0>();Structured binding declarationsAuto D={1,2};The type of OK:D is std::initializer_list<int>Auto N={5};The type of ok:n is std::initializer_list<int>//auto E{1, 2};//C++17 error, previously std::initializer_list<int> auto M{5}; //ok:c++17 the type of M is int, preceded by Initializer_list<int>//decltype (auto) z = {1, 2}//Error: {1, 2} is not an expression   //Auto is commonly used for nameless types, such as type of lambda expression auto lambda = [] (int x{return x + 3 }; //auto int x;//c++98 Legal, c++11 error //auto x;// c Legal, C + + error }            

Possible output:

A, before modification through C2 = 3a, after modification through C2 = 4

C + + Auto Properties

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.