Several major changes worth paying attention to in zz:c++11 __c++

Source: Internet
Author: User
Tags thread class
ZZ from:http://blog.csdn.net/lanphaday/article/details/6564162

Rai Yonghao (http://laiyonghao.com)
Disclaimer: This article derives from the article "the biggest Changes in c++11 (Why)", published by Danny Kalev on June 21, 2011, and almost all of the content has been moved, but not translated in full, and there is confusion , please refer to the original text (http://www.softwarequalityconnection.com/2011/06/the-biggest-changes-in-c11-and-why-you-should-care/).
Note: The author, Danny Kalev, was a member of the C + + standards committee. LAMBDA Expression

The form of a LAMBDA expression is this:
[CPP] view plain copy print? [Capture] (parameters)->return-type {body}
Take a look at some examples of several uppercase letters in a sequence of characters:
[CPP] view plain copy print?      int main () {char s[]= "Hello world!"; int uppercase = 0; Modified by the Lambda For_each (S, s+sizeof (s), [&uppercase] (char c) {if (Isupper (c)) uppercase       ++;    });   cout<< uppercase<< "uppercase letters in:" << s<<endl; }
The meaning of & in [&uppercase] is that the lambda function body gets a uppercase reference so that it can change its value, and if not, then uppercase passes the past in the form of a pass value.

automatic type inference and Decltype

In c++03, declaring an object must also indicate its type, and in most cases, declaring an object also includes an initial value, in which case the C++11 can let you declare the object without specifying the type:
[C-sharp]View Plain copy print? Auto x=0; 0 is the int type, so x is also the int type Auto c= ' a '; char Auto d=0.5; Double Auto National_debt=14400000000000ll;//long Long
This feature is useful when the type of the object is very large, such as:
[CPP]View Plain copy print?   void func (const vector<int> &vi) {vector<int>::const_iterator ci=vi.begin (); }
The iterator can be declared as:
[CPP]View Plain copy print? Auto Ci=vi.begin ();
C++11 also provides a mechanism for "capture" types from objects or expressions, and a new operator Decltype can "capture" the type of its result from an expression and "return":
[C-sharp]View Plain copy print?   Const Vector<int> VI;   typedef decltype (Vi.begin ()) CIT; CIT Another_const_iterator;

Unified Initialization Syntax

C + + has at least 4 different initialization forms, such as initialization in parentheses, see:
[CPP]View Plain copy print?   std::string s ("Hello"); int M=int (); Default initialization
There is also an equal form:
[CPP]View Plain copy print?   std::string s= "Hello"; int x=5;
For POD collections, you can also use curly braces:
[CPP]View Plain copy print?   int arr[4]={0,1,2,3}; struct TM today={0};
Finally, members of the constructor are initialized:
[CPP]View Plain copy print?    struct S {int x; S (): X (0) {}};
So many initialization forms, not only the rookie will make a big head, master also unbearable. What's worse is that c++03 cannot initialize class members of POD arrays, nor can the initial pod array when using new[], motherfucker. C++11 is eminence with braces:
[CPP]View Plain copy print?   Class C {int A;   int b;   PUBLIC:C (int i, int j);   }; C c {0,0}; C++11 only.    Equivalent to C C (0,0); int* a = new Int[3] {1, 2, 0};   /c++11 only class X {int a[4]; Public:x (): a{1,2,3,4} {}//c++11, initializing array member};
Another good thing is that for the container, you can finally get rid of the push_back () call, the C++11 can visually initialize the container:
[CPP]View Plain copy print?   c++11 container initializer vector vs<string>={"a", "second", "Third"}; Map singers = {"Lady Gaga", "+1 (212) 555-7890"}, {"Beyonce Knowles", "+1 (212) 555-0987"}};
The data member initialization in the class is also supported:
[CPP]View Plain copy print?   Class C {int a=7;//c++11 only Public:c (); };

deleted functions and defaulted functions

Functions like the following form:
[CPP] view plain copy print? struct a {a () =default;//c++11 virtual ~a () =default;//c++11};
Called defaulted function, =default; Instructs the compiler to generate the default implementation of this function. There are two advantages: one is to make the programmer easy, less to knock on the keyboard, and the other is to have better performance.
The opposite of the defaulted function is the deleted function:
[CPP] view plain copy print? int func () =delete;
This product has a large use is to implement Noncopyabe to prevent the copying of objects, in order to prohibit copying, with =deleted to declare two key member functions on it:
[CPP] view plain copy print?       struct Nocopy {nocopy & operator = (const nocopy &) = delete;   Nocopy (const nocopy &) = delete;   };   Nocopy A; Nocopy B (a); Compilation error, copy constructor is deleted function

nullptr

Nullptr is a new C + + keyword, which is a null pointer constant, which is used to replace high risk NULL macros and 0 literal quantities. Nullptr are strongly typed:
[CPP] view plain copy print? void f (int); #1 void F (char *);//#2//c++03 f (0);    Which f is the call? C++11 f (nullptr)//No doubt, the call is #2
All the pointers can be nullptr, including function pointers and member pointers:
[CPP] view plain copy print? const char *PC=STR.C_STR ();   Data pointers if (PC!=NULLPTR) cout<<pc<<endl; Int (A::* PMF) () =nullptr; pointer void (*PMF) () =nullptr that points to a member function; Pointer to function

Delegate Constructors

A constructor in C++11 can call another constructor of the same class:
[CPP] view plain copy print?    Class M//c++11 delegating constructors {int x, y;   Char *p; public:m (int v): X (v), y (0), p (new char [MAX]) {}//#1 target M (): M (0) {cout<< "Delegating ctor" <<e nd;} #2 Delegating
#2 is called a delegate constructor that invokes the real constructor #1.

Right Value reference

The reference type in c++03 is bound to only the left value, and c++11 refers to a new reference type called the right value reference type, which is bound to a right value, such as a temporary object or literal amount.
The main reason to increase the right value reference is to implement move semantics. Unlike traditional copies, move means that the target object "steals" the resources of the original object and places the source in an "empty" state. When you copy an object, it is expensive and unnecessary, and the move action replaces it. In the case of string swapping, there is a tremendous performance boost with move meaning, as the original scenario would be:
[CPP]View Plain copy print?    void Naiveswap (String &a, string & B) {string temp = A;    A=b;   B=temp; }
This is silly, naïve, slow, because you need to request memory and then copy the characters, and move only needs to exchange two data members without requesting, freeing memory, and copying character arrays:
[CPP]View Plain copy print? void Moveswapstr (string& empty, string & filled) {//pseudo code, but you get the idea size_t Sz=empty.siz    E ();   const char *p= empty.data ();    Move filled ' s empty empty.setsize (Filled.size ());   Empty.setdata (Filled.data ());    Filled becomes empty filled.setsize (SZ);   Filled.setdata (P); }
To implement a class that supports move, you need to declare the move constructor and the move assignment operator as follows:
[CPP]View Plain copy print? Class Movable {movable (movable&&);//move constructor movable&& (operator=); movable&& Ove assignment operator};
C++11 's standard library uses move semantics extensively, and many algorithms and containers have been optimized using move semantics.

C++11 's standard library

In addition to the new containers contained in TR1 (Unordered_set, Unordered_map, Unordered_multiset, and Unordered_multimap), there are new libraries, such as regular expressions, tuple, function object wrappers, and so on. Here are some c++11 new features of the standard library: line Threading

From a programmer's point of view, the most important feature of c++11 is concurrency. C++11 provides a thread class, also provides promise and future for synchronization in a concurrency environment, performs concurrent tasks with the async () function template, and thread_local stores data declared as exclusive to a particular thread, here (http://www.devx com/specialreports/article/38883) has a simple c++11 line threading Tutorial (English). new smart pointer class

The only smart pointer class Auto_ptr defined by c++98 has been deprecated, C++11 introduced new smart-targeted classes shared_ptr and unique_pt

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.