-9 reasons to start using C ++ 11

Source: Internet
Author: User

Http://www.oschina.net/news/28928/9-reasons-to-start-using-c11

 

If yourCodeWorking properly and performing well, you may want to know why C ++ 11 is required. Of course, it feels good to use the latest technology, but is it actually worth it?

In my opinion, there is no doubt that the answer is yes. The following nine reasons are given: performance advantages and development efficiency.

Benefits

Reason 1:Move semantics (move semantics ). Simply put, it is a way to optimize replication. Sometimes replication is obviously a waste. If you copy content from a temporary String object, simply copying the pointer to the character buffer is much more efficient than creating a new buffer and then copying it. It works because the source object is out of scope.

However, in the past, C ++ did not determine whether the source object is a temporary object. In addition to the copy operation, the move semantics allows you to have a move constructor and a move assignment operator.

Do you know? When you use a standard library class such as string or vector in Visual Studio 2010, they support the move semantics. This prevents unnecessary replication to improve performance.

By implementing the move semantics in your class, you can get extra performance improvements, such as when you store them in STL containers. Also, the move semantics can be applied not only to constructors, but also to methods (such as the push_back method of vector ).

Reason 2:By using category attributes (type traits, such as is_floating_point) and template metaprogramming (such as enable_if template), you can customize templates for Certain types, which can be optimized.

Reason 3:Hash is implemented in a standard way. It provides faster insertion, deletion, and search, which is useful when processing a large amount of data. You can now use unordered_map, unordered_multimap, unordered_set, and unordered_multiset data structures.

Improve Efficiency

Improving efficiency is not only in terms of code performance, but also in terms of development time. C ++ 11 can make your code shorter, clearer, and easier to read, which improves your efficiency.

Reason 4:The auto keyword can automatically infer the type, so the following code:

 
Vector <vector <mytype> >:: const_iterator it = V. Begin ()

 

Now you can write it as follows:

 
Auto it = V. cbegin ()

Although some people may say that it hides type information, in my opinion, it is more advantageous than it is because it reduces visual mixing and shows code behavior, it also allows you to write less words!

Reason 5:Lambda expressions provide a way to define an anonymous method object (actually a closure), which is more linear and regular in the code. This is in connection with STLAlgorithmEasy to use in combination:

 
Bool is_fuel_level_safe () {return all_of (_ tanks. begin (), _ tanks. end (), [this] (tank & T) {return T. fuel_level ()> _ min_fuel_level ;});}

Reason 6:The new smart pointer replaces the problematic auto_ptr, so you don't have to worry about memory release and remove the Code related to memory release. This makes the code clearer and eliminates Memory leakage and time for searching for memory leaks.

Reason 7:Using methods as first class object is a very powerful feature, which makes your code more flexible and generic. C ++'s STD: function provides this feature. The method provides a way to package and pass any callable thing-function pointer, function imitation (functor), lambda expressions, and so on.

Reason 8:There are many other small functions, such as override, final keywords, and nullptr, to make your code intent clearer. For me, reducing visual confusion and expressing my intentions more clearly in code means being happier and more efficient.

Another aspect of development efficiency is error detection. If your error occurs during running, it means you need to run the software at least and may have to reproduce the error through a series of steps, which takes time.

C ++ 11 provides a way to check the prerequisites and capture errors as early as possible-before you run the code during compilation. This isReason9.

This is achieved through static assertions (static_assert) and Class Attribute templates. Another advantage of this method is that it does not need to occupy any runtime overhead and has no performance loss!

Now start to master C ++ 11

In addition to the above descriptions, more changes and new features are described in the C ++ 11 standard. It needs to be described in a whole number. However, I believe they are worth your time to learn. You will save time on improving efficiency. Many mainstream compilers have begun to support some standards of C ++ 11. What are you waiting? Start!

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.