about C++14: The new features you need to know
new and useful features that make C + + more secure and convenient
This August, after a vote, the C++14 standard was unanimously adopted. The only remaining job is the ISO for the official release of C + + standards. In this article, I focus on several important points in the new standard, showing how upcoming changes will affect your programming, especially when using modern C + + as an idiom and paradigm.
The C + + Standards Committee is determined to make the standard-setting process faster than the last 10 years. This means that c++14, which is only 3 years from the previous standard (i.e. c++11), is a relatively small publication. This is far from disappointing news, on the contrary, this is good news for programmers. Because of this, developers can keep up with the new features in real time. So today you can start using the new features of c++14-and if your tool chain is flexible enough, you can almost use all the new features.
You can now get a free copy of the draft standard from here. Unfortunately, the ISO will charge when the final version of the standard is released.
Shortening the time interval for standard publishing can help compiler writers keep up with language changes more in real time. Released again only three years later, there are fewer changes that need to be adjusted to accommodate it.
The examples in this article are mainly tested on Clang 3.4, and clang 3.4 covers the new features of most c++14. Currently, g++ has less coverage for new features, and Visual C + + seems to be lagging behind. c++14: Major changes
Next, this article will illustrate the c++14 features that have a significant impact on the programmer's coding effort, and when and where to use these features is discussed at the same time as examples. return type derivation
In this release, the role of the keyword auto expanded. The C + + language itself is still type-safe, but the mechanism for type safety is gradually being implemented by the compiler rather than by the programmer.
In C++11, the original use of auto by the programmer is used for declaration. This is especially useful for the creation of an iterator, because the complete and correct type name may be scary. C + + code that uses auto is much easier to read:
1 |
for (Auto II = Collection.begin ();.. |
In c++14, the use of auto has been expanded in several ways. One of these is the derivation of the return type, which is of extraordinary significance. Write the following line of code in a function: This code is still completely type-safe because the compiler knows what type the begin () should return in the context. Therefore, the type II is undisputed, and in every place where I use II, the compiler will check.
For programmers and compilers, it is clear that the function returns a double type. So in c++14, programmers can use auto instead of double to define function return types:
One detail that needs to be noted for this new feature is fairly easy to understand. That is, if a function has multiple return paths, the value returned by each return path needs to have the same type.