Several new features in the C++11

Source: Internet
Author: User

C++11 Standard has been determined, in addition to a lot of library functions, in the syntax convenient also got a lot of enhancements. Here are a few of the syntax that I like more:

Automatic type Deduction Auto

C + + is now finally supporting C #-like Var keywords at the compiler level, in C + + the keyword is auto, the basic usage is as follows:

    auto i = 0;         //int
     auto c =  ' C ' ;    < Span style= "" >//CHAR
    auto  s =  "Hello World" //const char*

One of the very intuitive benefits of the Auto keyword is that we can simplify the iterator in the STL container traversal:

for(auto it = V.begin (); It! = V.end (); it++)
{
cout << *it << Endl;
}

Lambda expression

Lambda expressions are primarily used to construct anonymous functions, which can replace most of the scene's functor (feeling that the std::bind just introduced into the STL library is also handy for seconds), and has better readability. A simple example is as follows:

    auto k = [] (int  x, int  y "{ return x  + y;};
    cout << K (3, 2) << Endl;

as you can see, its basic syntax is:  [capture" (parameters) {body} [capture]   What is the role of

int i1 = 0, i2 = 3;
Auto k = [&i1, &i2] () {i1 = 3; i2 = 5;};
cout << i1 << " << i2 << Endl;

In addition to the ordinary local variables described earlier, it can also be the following special symbol:

    • = All local variables to be passed by value (including the This pointer)
    • & all local variables to be passed by reference (including the This pointer)
    • This this pointer

Range-based For-loop

This is actually like a foreach in C #, but it doesn't introduce a new keyword, it's a direct use for

int P[8] = {2, 3, 5, 7, 11, 13, 17, 19};
for (Auto& i:p)
{
printf ("%d", i);
}

In addition to supporting arrays, it also supports iterator traversal in STL, For_each functions can basically be laid off. As for its principle, can refer to this article.

Enum class

In the C language, the enumeration is equivalent to a numeric constant, and the compiler does not handle it too much, and in C++11, the Enum class is introduced to replace the enumeration, which is defined in the following way:

    ENUM&NBSP; class  color { red,  Blue};
    ENUM&NBSP; class   fruit { banana,  Apple

In addition to adding a class, there is no other change, but it defines the enumeration function similar to the enumeration in C #. Compared with the traditional C language enumeration, there are two main differences:

    1. Strong-Type syntax checking
    2. Enumeration names only need to be unique within the class range

A strongly typed syntax check can effectively prevent illegal conversions and comparisons between enumerations,Color::Red = = Fruit::Banana like The judgment cannot be compiled through.

The enumeration name, which only needs to be unique within the class range, effectively shortens the length of the enumeration, and in the case of the Color enumeration, in C, in order to prevent ambiguity and naming conflicts, it is often necessary to define the form color_red, Color_blue.

Static Assertion Static_assert

Static assertions mainly provide assertions that can be determined in the implementation of the compilation period in code, and if errors are displayed directly in the form of compilation errors, thus enhancing the robustness of the program. This is actually the previous boost.static_assert, but it has become more friendly with the support of the compiler. Examples are as follows:

static_assert(sizeof(int) = = 4, "int needs to is 4 bytes to use this Code");

Closed class and Closed method

C++11 also introduces a method similar to the C # seal keyword to implement closed classes and closed methods to prevent object inheritance, method overloading. But its keyword is final, similar to Java.

Final Class

     class BASE&NBSP; final
&NBSP;&NBSP;&NBSP;&NBSP;{
    }; 
    < Span style= "" >class derived : public  base // inheritance closed class, syntax error
    {
  &NBSP;&NBSP;&NBSP,};

Final method

class Base
{
virtual void A () Final ;
};
class Derived : public Base
{
virtual void A (); //Rewrite containment method, compile error
    };

Explicit overrides

For a virtual function override, you can explicitly override the keyword in C + + 11 for a more rigorous syntax check.

ClassBase
{
VirtualvoidAFloat=0.0);
Virtual void b ()  const;
        virtual void  c ();
        void D ()
    };
    class Derived : public base
    {
         virtual void a (int =0)   override; //definition inconsistencies, compilation errors
virtual void B ()          override; //The return type is notConst, compilation error
virtual void C ()          override; //correct
void D ()                  override; //not a virtual function, compilation error
    };

PS: I think this grammar check should be added by default, explicitly remove the check by keyword, instead of explicitly opening the check like this. The devil's upward compatibility.

Other

There are a few other grammar I also like, such as the cancellation of string escape, delegate constructors, deleted and defaulted member functions, unified initialization syntax, etc., but these are not supported in the VC, can not achieve cross-platform development, temporarily do not introduce.

Several new features in the 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.