Some powerful new features in C + + version 11th summary _c language

Source: Internet
Author: User
Tags anonymous

Automatic type derivation of auto type deduction

The Auto keyword allows the user to use the built-in type inference feature of C + +.

std::string something = somethingthatreturnsastring.getstring ();
Auto something = somethingthatreturnsastring.getstring ();

The Auto keyword automatically infers the above argument (something) to be a string-type conclusion and replace it with the correct type where auto appears. This feature is particularly useful for iterators.

for (Std::vector<t>::iterator it = X.begin (); it!= x.end (); i++)
{
  it->something ();
}

The above code can be written as:

for (Auto it = X.begin (); it!= x.end (); i++)
{
  it->something ();
}

Praise! The code looks much simpler!

Strongly Typed Enums strongly typed enumeration

This feature effectively avoids naming conflicts of enumerated types, eliminating many potential bugs. In older versions of C + +, programmers had to set a globally unique name for each enumerated item. For example, if you name an enumerated item named None, then the other enumeration collection cannot use that name anymore. But now, you can do it! The example given below is not very much like the text above//myenum:: Is there any difference between all and myenum::all? )

Enum class MyEnum {None, one, all};
MyEnum o = myenum:: all;
Auto p = myenum::all; 
Equally effective

Lambdas expression

A LAMBDA expression is simply an anonymous function (the original in-place function, meaning "functions to be inlaid", but an anonymous function can express this meaning more precisely). Useful for iterators and for loops, which you need to use only once in a program, so there's no need to specifically define it in a program. Lambda expression does not make C + + in the logical expression of "and the past is not possible" degree, it is a function of the idea of programming to introduce the language characteristics, can make the program more compact. The simplest form of a LAMBDA expression is the following:

[]() { }

Plus all the possible operators, it would be this:

[] () mutable-> T {}
Where [] is the capture list, () is the argument list, {} is the function body

Capture List Capture lists

The capture list defines what types of things can be matched from a LAMBDA expression to a function body. You can include the following:

A value: [x]
A reference [&x]
Reference to any variable in the current scope [ampersand]
Same as 3, but by the value of the variable
You can mix any of the above, as long as you are separated by commas [x, &y]

Argument List parameter lists

parameter lists and parameter lists for C + + functions are a concept.

function body

A function body is the code that is actually executed when a LAMBDA expression is invoked.

Return Type Deduction

return value Inference

If the LAMBDA expression has only one return declaration, then the return value type can be omitted and the type is implicitly typed: Decltype (return_statement)

Variable Labmda

If a LAMBDA expression is marked as mutable (for example: [] () mutable{}), the value is allowed to be modified in the function body for values captured by value.

Here's an example:

int main ()
{
  char s[]= "Hello world!";
  int uppercase = 0; 
Lambda changes the value of this variable
  for_each (S, s+sizeof (s), [&uppercase] (char c) {
  if (Isupper (c))
   uppercase++;
  });
 cout<< uppercase<< "uppercase letters in:" << s<<endl;
}

Unique pointer

The Unique pointer is a c++11 version of the Smart pointer class.

Once you have defined an object with the UNIQUE_PTR keyword, the object is destroyed and the memory is freed when one of the following events occurs:

Unique_ptr managed objects are destroyed.
The Unique_ptr managed object points to another pointer through the assignment operator, or calls the Reset () method.
For those who do not want to know too much detail, this means that if you use the semantics of the unique pointer, you do not have to manually recycle the object's memory until you jump out of the scope.

Previously, we needed to write code like this:

Yourobject * obj = new Yourobject ();

Then at the end of the program you must remember to release the memory:

Delete (obj);
Otherwise you can cause a memory leak. And now,

Std::unique_ptr<yourobject> obj (new Yourobject ());

When obj jumps out of scope, memory is automatically recycled.

Static_assert

Static_assert is simply an assertion that executes at compile time. For example, you can do this:

Static_assert (sizeof (unsigned int) * char_bit = 32);

Assuming that the above logical judgment fails because of the system's cause, Static_assert will assert failure.

Another use of it is to use it with C + + feature types. Like what:

Static_assert (Std::is_pod<yourstruct>::value, "not a pod struct!");

POD refers to the "simple Data" (PLAIN) structure, that is, it is a class (you can use the struct keyword definition or the class keyword), but there are no constructors, destructors, and virtual member functions. So, if a stupid rookie programmer tries to add a constructor to this type, Static_assert will block the behavior at compile time and make an error. This is very useful for code maintenance.

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.