C++11 Automatic derivation of auto

Source: Internet
Author: User

C++11 Automatic derivation of auto

The auto introduced in C++11 has two main purposes: automatic type deduction and return value placeholder.

The semantics of the identity temp variable in auto in C++98, which has been removed in c++11 because of its minimal use and redundancy. Before and after two standard auto, is completely two concepts.

Automatic type deduction

Automatic type deduction for auto, used to infer the data type of a variable from an initialization expression. The automatic type deduction of auto can greatly simplify our programming work.

Auto actually compiles the variables at compile time, so it does not adversely affect the running efficiency of the program. In addition, auto does not affect the compilation speed, because the compile-time would have to be the right derivation and then determine whether to match the left.

#define_crt_secure_no_warnings#include<iostream>#include<string>#include<vector>#include<map>//3. When using template technology, if the type of a variable depends on the template parameter, it is difficult to determine the type of the variable without using autoTemplate <typename T, TypeName u>voidMultiply (T T, u u) {Auto V= t * u;//when auto is used, it is automatically determined by the compiler}classstudent{ Public:    Static intvar1; //auto var2; error, non-static member variable//static auto Var3; error, initial value required};intSTUDENT::VAR1 =Ten;//void Fun (auto x = 1) {} error, auto function argument, some compilers cannot compilevoidmytest () {//auto A; error, no initialization expression, cannot infer type of a//Auto int a1 = 0; error, the semantics of auto temp variable no longer exist in c++11, which is the usage of the old standard. //1. Automatic Help deduction typeAuto A =Ten;//a---> intAuto C ='A';//c---> CharAuto S ("Hello");//---- const char *//2. Lengthy typestd::map<int, std::map<int,int> >MAP_; Std::map<int, std::map<int,int> >::const_iterator ITR1 =Map_.begin (); ConstAuto ITR2 =Map_.begin (); Auto PTR= []()//ptr---> void ptr (){std::cout<<"mytest ..."<<Std::endl; }; //lambda expression        Charx[3]; Auto y= x;//y---> char *//Auto degrades the exponentially pointer to an array unless it is declared as a reference//Auto Z[3] = x; error, auto array, failed to compile    return;}intMain () {mytest (); System ("Pause"); return 0;}

2. Precautions for use

1, we can use Valatile,pointer (*), reference (&), rvalue Reference (&&) to decorate auto
Auto k = 5;
auto* PK = new auto (k);
auto** PpK = new Auto (&k);
Const Auto n = 6;

2. Variables declared with auto must be initialized
Auto m; M should be intialized

3. Auto cannot be combined with other types
auto int p; This is the practice of old auto.

4. Function and template parameters cannot be declared as auto
void MyFunction (auto parameter) {}//No auto as method argument
Template<auto t>//utter nonsense-not allowed
void Fun (T-t) {}

5. Variables defined on the heap, expressions using auto must be initialized
int* p = new Auto (0); Fine
int* pp = new auto (); should be initialized
Auto x = new Auto (); No Intializer
auto* y = new auto (9); Fine. Here y is a int*
Auto Z = new Auto (9); Fine. Here Z is a int* (It isn't just an int)

6, because auto is a placeholder, not a type of his own, so it cannot be used for type conversion or some other operations, such as sizeof and typeid
int value = 123;
Auto x2 = (auto) value; No casting using auto
Auto x3 = static_cast<auto> (value); Same as above

7. Variables defined in an auto sequence must always be deduced into the same type
Auto X1 = 5, x2 = 5.0, x3= ' R '; This is too much....we cannot combine

8. Auto cannot be automatically deduced into Cv-qualifiers (constant & volatile qualifiers) unless it is declared as a reference type
const int i = 99;
Auto J = i; j is int, rather than const int
j = +//Fine. As J is not constant
Now let us try to have reference
auto& k = i; Now-K is const int&
k = 100; Error. K is constant
Similarly with volatile qualifer

9, Auto will degenerate exponentially pointer to array, unless declared as a reference
int a[9];
Auto J = A;
Cout<<typeid (j). Name () <<endl; This would print int*
auto& k = A;
Cout<<typeid (k). Name () <<endl; This would print int [9]

C++11 automatically infer auto

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.