C++11 Series-What is c++11

Source: Internet
Author: User
Tags new set

What is c++0x?

C++0X is the name used in the standardization process for the latest standard in C + +, and in this series of articles we will present a new set of language features added to the latest standards. In September 2011, c++0x officially issued and named C++11, and now many compilers have supported some c++11 features.

C++11 includes a number of new features: Key features like lambda expressions and move semantics, utility type derivation keyword auto, simpler container traversal methods, and lots of improvements that make templates easier to use. This series of tutorials will contain so the above features.

Should you pay attention to c++11?

Obviously, C++11 brings a lot of new features to C + +. C++11 will fix a large number of defects and reduce code procrastination, such as the support of lambda expressions to make the code more concise. This feature, like mobile semantics, improves the base efficiency of the language kernel, allowing you to write faster code. Optimization of the template system makes it easier to write generic code.

The new standard library will also contain new features, including support for multithreading and optimization of smart pointers, which will provide simpler memory management methods for those who do not have the same boost::shared_ptr.

I've started using the new c++11 feature, and I love it: the new auto keyword, support for template ' >> ' notation, lambda expressions and new function definition syntax.

How has c++11 been developed?

The advent of c++11, first of all thanks to the work of the C + + Standards Committee, a group of experts from academia and industry, their many meetings to overcome the problem, and finally designed a cross-platform, supported by a variety of compilers, can generate efficient and easy to maintain code language. and c++11 new standards, like an incredible extension of the flexible and powerful C + +.

What does c++11 include? Easier to use language

After using c++11, I found that it provides a number of basic methods to make C + + a more accessible language. This is not to say that it becomes a simple language-there are plenty of new features-there are plenty of ways to make programming easier. Let's look at an example of the Auto keyword. In C++11, if the compiler can get its type from the initialization of a variable, you don't need to specify the type. For example, you can write:

int x = 3;auto y = x;

The compiler can infer that the type of y is int. Of course, this is not a shining example that proves that auto is useful. Auto works well when using templates, especially STL. Why say that, imagine using an iterator (iterator):

map<string, string> address_book;address_book["Alex"] = "[email protected]";//add a bunch of people to address_book

Now you want to iterate through the elements in the Address_book, to do this, you need an iterator:

map<string, string>::iterator iter = address_book.begin();

This is a scary long-type statement when you already know this type. This is not a lot more concise:

auto iter = address_book.begin();

The code became simpler and clearer, and I felt more readable because the template syntax blurred the rest of the line. This is one of my favorite features, and I found it eliminates many headaches and hard-to-follow compilation errors, saving time without losing the meaning of expressions.

Interval iteration (range-based for loop)

An example of the following iterator is a better way for c++11 to deal with iterations, some called interval iterations (basically all modern languages support). This example is enough to prove how elegant this syntax is:

Vector<int> vec; vec. Push_back (10 vec. Push_back (20 for  (int & i: vec {< Span class= "line", cout<<i;                /span>                

All you need to do is give a variable and the interval to iterate over. But what if you want to iterate over a map? How do you set the type of the values in the map? You know the type of the vector value is int. But the value type of map is pair, which gives you keys and values through. First and. Second. But with auto, you don't have to care about the exact type, you can simply write:

for (auto address_entry: address_book){ cout<<address_entry.first<<" "<address_entry.second<<endl;}

This will print out:

Alex [email protected]

This is a good c++11 new feature combination usage right?

>> (Right angle brackets)

I also have an easier-to-use optimization-in the previous C + + standard, if you write a template that contains other template types:

vector<vector<int> > vector_of_int_vectors;

You must add a space between the end of the two ' > '. This is not only annoying, but when you write >> without spaces, you get confusing and misleading compilation error messages. The reason for this behavior is the maximum matching principle for C + + lexical analysis (maximal munch rule). The good news is that from now on, you'll never have to worry:

vector<vector<int>> vector_of_int_vectors;

Yes, it's really a small thing, but it's a manual code that overcomes the triumph of machine tools. In addition, this kind of writing is not so ugly.

Multithreading

For the first time, C++11 will contain a memory model and a corresponding multithreaded library, which means you will be able to write multithreaded code that conforms to the standard. The new standard will provide all common threading methods, such as threading, thread-local storage, and atomic operations. It also offers a range of interesting features: Futures and promises. The main idea of futures and promises is that you can write code that says, "this object, the result of a future code that has not been calculated," and it will be able to calculate the results in the background. When this value is needed, you send a request to the future, and if the value is ready, then you can get it, or you will continue to wait.

I'll delve into multithreading in a follow-up article.

And a lot of other stuff.

The number of c++11 characteristics is objective. You can read the c++11 page in Wikipedia, and I plan to delve into these features in this series of articles, including:

    • How to write better code with auto, Decltype, and new function syntax
    • Lambda expression
    • Interval iterations
    • Constant-expression
    • Rvalue Reference and move semantics
    • Nullptr and strongly-typed enumerations
Compiler support for C++11

Of course, the good news is that a large number of compilers have started to support c++11 if there is no good language feature to use. The Apache Foundation has compiled a list of c++11 language features and compiler support: compilers that support c++11. If you are interested in gcc, here are GCC4.7 support c++11.

Some compilers, such as GCC, do not support these new features by default-for example, to support the C++11 feature, you must specify-std=c++0x at compile time. Of course this is also useful, you can choose the compiler and the language set in your project.

from:http://towriting.com/blog/2013/08/01/what-is-cpp11/

C++11 Series-What is 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.