C++0x: The future of C++

来源:互联网 发布:企业ip网络设计拓扑 编辑:程序博客网 时间:2024/06/03 18:39

原文链接http://www.cprogramming.com/c++11/what-is-c++0x.html

What is C++0x?

C++0x is the working name for the new standard for C++, adding many language features that I'll cover in this series on C++0x. Just days ago, a C++0x draft was voted on and finalized, meaning that C++0x is almost certain to be C++11, and many compilers now provide support for some of the core C++0x features. Final publication of the standard itself is expected in mid-2011, with the final standard likely being called C++11. Until the final name change, I'll refer to the new draft as C++0x. [Update: according to Herb Sutter, the standard is confirmed as C++11 and should be published in a matter of weeks as of mid-August.]

C++0x includes a wide range of features: major new features like lambda support and "move semantics", usability improvements like type inference through the auto keyword, simplified looping over containers, and many improvements that will make templates easier to write and easier to use. This series on C++0x will cover all of these features and many more.

Should you care about C++0x?

Most definitely. C++0x adds many new language features to C++. C++0x should fix many annoyances and reduce the overall verbosity of C++ as well as provide new tools, such as lambda expressions, that increase its overall expressiveness and clarity. Fetaures like move semantics improve the basic efficiency of the language, allowing you to write faster code, and the improvements to the template system make it much easier to write generic code.

The new standard library will also include many new features, including adding multithreading support directly into C++ and improved smart pointers that will simplify memory management for those who aren't already using features like boost::shared_ptr.

I've started using several new C++0x features professionally and I'm loving it. Some of the new features I'm fond of include the new meaning of the auto keyword, simplifications like better handling of right angle brackets in templates, lambda expressions and the new function declaration syntax.

How was C++0x developed?

I can't go on further about C++0x without acknowledging the hard work done by the C++ standards committee--a group of experts from academia and industry who have met many times to work through all the edge cases and design a programming language that can be implemented across multiple platforms by multiple compilers, producing efficient and reasonably maintainable code. The next standard, C++0x, looks to be a fantastic addition to the flexibility and power of C++.

What is C++0x about?

Language usability

Having started to use C++0x, I'd say that the most fundamental way of looking at it is that it makes C++ a much more usable language. This isn't to say that it makes it a simpler language--there are lots of new features--but it provides a lot of functionality that makes it easier to program. Let's look at one example, the auto keyword.

In C++0x, if the compiler is able to determine the type of a variable from its initialization, you don't need to provide the type. For example, you can write code such as

int x = 3;auto y = x;
and the compiler will deduce that y is an int. This, of course, isn't a shining example of where auto is really useful. Auto really comes into its own when working with templates and especially the STL. Why is that? Imagine working with an iterator:
map<string, string> address_book;address_book[ "Alex" ] = "webmaster@cprogramming.com";// add a bunch of people to address_book
Now you want to iterate over the elements of the address_book. To do it, you need an iterator:
map<string, string>::iterator itr = address_book.begin();
That's an awfully long type declaration for something that you already know the type of! Wouldn't it be nice to simply write:
auto itr = address_book.begin();

The code is much shorter, and frankly, I think it's more readable, not less, because the template syntax obscures everything else on that line. This is one of my favorite new features, and I find it eliminates a lot of headaches and hard-to-track-down compiler errors, and just generally saves time without losing expressiveness.

Ranged For Loops

Now, the iterator example is one where C++0x has come up with an even better way of handling this--something called a range-based for loop (which almost every language has nowadays). The idea is so elegant, an example should suffice:

vector<int> vec;vec.push_back( 10 );vec.push_back( 20 );for (int &i : vec ) {        cout << i;}

All you need to do is give your variable and the range to iterate over (defined as something with iterators available via calls to begin and end--so all STL containers) and you're set! This is a pretty new feature, available as far as I know only in GCC 4.6.

But what if you want to iterate over a map? How do you put in the type for a value stored in a map? With a vector, you know the value is an int. With a map, it's essentially a pair, with .first and .second giving you the key and value. But with auto, we don't need to worry about getting the exact type right, you can simply do this:

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

Which prints out as:

Alex <webmaster@cprogramming.com>

Isn't that a nice combination of new features in C++0x? It feels like it was designed that way :)

Mulithreading

For the first time, the C++0x standard will include a memory model and corresponding libraries for multithreading, meaning that you'll be able to write standards-compliant multithreading code. The new standard will provide for all the normal threading functionality, such as threads and thread-local storage and atomic operations. It will also include an interesting set of features, futures and promises. The basic idea of futures and promises is that you can write code that says, "this object, a future, stands for a result that hasn't been computed yet" and the work to compute the value can take place in the background. When the value is needed, you ask the future for it; if the value is ready, you get it; if not, you wait.

I'll go into more depth on mulithreading in a later article in this series.