Should You Care About C++11?: Boost::shared - PTR
Should You Care About C++11?: Boost::shared - PTR
Most definitely. C++11 adds many new language features to C++. C++11 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++11 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.
Language usability
Having started to use C++11, 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++11, 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" ] = "[email protected]";
// 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.
Now, the iterator example is one where C++11 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 );