The Autokeyword of new characteristics of c++11

Source: Internet
Author: User

C++11 is an extension and correction of the current C + + language. C++11 contains a number of new features: Contains lambda expressions, type derivation Keywordauto, Decltype, and a number of improvements to templates.

g++ compile c++11 command plus

-std=c++11

The first function of introducing auto in C++11 is to derive the active type.
Auto's own active type deduction, which is used to determine the data type of a variable from an initialization expression.

By auto's own active type deduction, we can simplify our programming work

Auto actually compiles the variables at compile-time, so it does not adversely affect the execution efficiency of the program. In addition, it seems that auto does not affect the compilation speed, because the compile time is also the right side deduction and then infer whether the left side matches.


#include <iostream> #include <typeinfo> #include <vector>using namespace Std;template<class T, Class u>void func (T T, u u) {auto ret = t*u;} int main () {//auto A;   Error, Auto is a row type deduction through an initialization expression, assuming there is no initialization expression, there is no way to determine the type of a auto i = 1;cout << typeid (i). Name () << Endl; Output is i,g++ compiler output i is int type, MS's vs2008 output is intauto d = 1.0;cout << typeid (d). Name () << Endl; Output is d,double type auto str = "Hello c++11"; cout << str << endl;vector<int> iv;iv.push_back (1); iv.push_ Back (2); Iv.push_back (3), for (auto Itr=iv.begin (); Itr!=iv.end (); itr++)//self-derivation for iterators cout << *itr << Endl; return 0;}

Other than that. When using the template technology. Assuming that the type of a variable depends on the template parameters, it is very difficult to determine the type of the variable (after using auto, it will be determined by the compiler itself).

The following is a detailed example.

Template <class T, class u>void Multiply (T T, u u) {Auto V = t*u;}




The Autokeyword of new characteristics of c++11

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.