The C ++ 11 standard has been determined. In addition to adding many library functions, the syntax is also improved. I like the following syntaxes:
Auto type deduction
Now c ++ supports the var keyword similar to C # At the compiler level. The keyword in c ++ is auto. The basic usage is as follows:
Auto I = 0; // int
Auto c = 'C'; // char
Auto s = "hello world"; // const char *
An intuitive benefit of the auto keyword is that we can simplify the iterator in stl container traversal:
For (auto it = v. begin (); it! = V. end (); it ++)
{
Cout <* it <endl;
}
Lambda expressions
Lambda expressions are mainly used to construct anonymous functions. They can replace the imitation functions in most scenarios (it feels like the std: bind that has just been introduced to the STL library is also easy for seconds ), and have 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 format is: [capture] (parameters) {body}. the syntax of the next two parts and the C # anonymous function is very good, but the first [capture]
What is the role? It is used to reference local variables outside the expression, for example:
Int i1 = 0, i2 = 3;
Auto k = [& i1, & i2] () {i1 = 3; i2 = 5 ;};
Cout <i1 <"" <i2 <endl;
In addition to the common local variables described above, they can also be the following special symbols:
- = All local variables are passed by value (including the this pointer)
- & All local variables are passed by reference (including the this pointer)
- This pointer
Range-based for-loop
This is actually like the foreach in C #, but it does not introduce new keywords, but directly uses
Int p [8] = {2, 3, 5, 7, 11, 13, 17, 19 };
For (auto & I: p)
{
Printf ("% d", I );
}
In addition to arrays, it also supports iterator traversal in stl, And the for_each function can basically be laid off. For how it works, refer to this article.
Enumeration class
In C, enumeration is equivalent to a numerical constant, and the compiler does not process it too much. In C ++ 11, enum class is introduced to replace enumeration, it is defined as follows:
Enum class
Color {Red, Blue };
Enum class
Fruit {Banana, Apple };
There is no change except adding a class, but the enumeration function defined by it is similar to that defined in C. Compared with the traditional enumeration in C language, there are two main differences:
- Strong type syntax check
- The enumerated name must be unique within the class range.
The strong-type syntax check can effectively prevent illegal conversions and comparisons between enumerations. The judgments such as Color: Red = Fruit: Banana cannot be compiled.
The enumeration name only needs to be unique within the class range, which effectively shortens the enumeration length. For Color enumeration, in C language, in order to prevent ambiguity and Name Conflict, the format must be defined as Color_Red and Color_Blue.
Static assertions static_assert
Static assertions mainly provide assertions that can be determined during the compilation period in the Code. If an error occurs, it is displayed in the form of a compilation error, thus enhancing the robustness of the program. This is actually the previous boost. static_assert, but it becomes more friendly with the support of the compiler. Example:
Static_assert (sizeof (int) = 4, "int needs to be 4 bytes to use this code ");
Sealing and sealing methods
In C ++ 11, a method similar to the C # seal keyword is introduced to implement a closed class and a closed method to prevent Object Inheritance and method overloading. However, the keyword is final, which is similar to java.
Final class
Class
Base final
{
};
Class
Derived: public
Base
// Inherit the closed class with syntax errors
{
};
Final Method
Class
Base
{
Virtual
Void A () final;
};
Class
Derived: public
Base
{
Virtual
Void A (); // rewrite the closed method, compilation Error
};
Explicit Overwrite
For the rewriting of virtual functions, explicit keyword overwrites can be used in c ++ 11 to obtain more rigorous syntax checks.
Class
Base
{
Virtual
Void A (float = 0.0 );
Virtual
Void B () const;
Virtual
Void C ();
Void D ();
};
Class
Derived: public
Base
{
Virtual
Void A (int = 0) override; // inconsistent definitions, compilation Error
Virtual
Void B () override; // The returned type is not const and the compilation is incorrect.
Virtual
Void C () override; // correct
Void D () override; // compilation error because it is not a virtual function
};
PS: In my opinion, this syntax check should be added by default. The check should be explicitly removed through the keyword, instead of opening the check explicitly like this. The ultimate compatibility.
Others
I also like other syntaxes, such as canceling string escaping, Delegate constructor, deleted and defaulted member functions, and unified initialization syntaxes. These are not supported in VC, cross-platform development is not supported.
Supplement: some of my favorite syntaxes in C ++ 11 (ii)