1. Reasons to start using C++11
If our code is working fine and performing well, everyone will be wondering why
you should jump on the C++11? Sure, it feels nice to be using the latest
technology, but is it actually worthwhile? In my opinion, the answer is a
definite yes. I will add some reasons to get into C++11 below. Reasons will be
aligned comparing performance benefits and developer productivity.
Get performance benefits
1. Move Semantics. To explain the concept very briefly, it s a way to
optimize copying. Sometimes copying is obviously wasteful. If you re copying
from a temporary string object, simply copying the pointer to the character
buffer would be much more efficient than creating a new buffer and copying the
character. It would work because the source object is about to go out of scope.
However, previously there was no mechanism in C++ to figure out whether the
source object is a temporary or not. Move semantics provide this mechanism by
allowing you to have a move constructor and a move assignment operator in
addition to the copy operations. Move semantics helps prevent unnecessary
copying and therefore improve performance.By implementing move semantics in your
own classes you can get additional performance improvements, for example when
you store them in STL containers. Also, keep in mind that move semantics can be
applied not only to constructors, but also to methods (such as
vector spush_back).
2. type traits (e.g. is_floating_point) and template metaprogramming (e.g.
the enable_if template), you can specialize your templates for types with
particular characteristics and thus implement optimizations.
3. The hash tables which have now become standard provide faster insertion,
deletion and lookup than their ordered counterparts, which can be very useful
when handling large amounts of data. You now have unordered_map,
unordered_multimap, unordered_set andunordered_multiset at your disposal.
Improve your productivity
It s not all about the performance of your code though. time is valuable too,
and C++11 can make you more productive by making your code shorter, clearer and
easier to read.
4. The auto keyword provides type inference, so instead of
vector<vector<MyType>>::const_iterator it = v.begin()
you can now simply write
auto it = v.cbegin()
Although some people complain that it obscures type information, in my opinion
the benefits are more important, as it reduces visual clutter and reveals the
behavior that the code expresses. And there s a lot less typing!
5. Lambda expressions provide a way to define anonymous function objects
(which are actually closures) right where they are used, thus making the code
more linear and easier to follow. This is very convenient in combination with
STL algorithms:
bool is_fuel_level_safe()
{
return all_of(_tanks.begin(), _tanks.end(),
[this](Tank& t) { return t.fuel_level() > _min_fuel_level; });
}
6. The new smart pointers which have replaced the problematic auto_ptr allow
you to stop worrying about memory cleanup, and to remove the cleanup code.
It s good for clarity, and for avoiding memory leaks and the associated time
spent to hunt them down.
7. Having functions as first class objects is a very powerful feature that
allows your code to be flexible and generic. C++11 makes a step in this
direction with std::function. Function provides a way to wrap and pass around
anything callable   function pointers, functors, lambdas and more.
8. There are many other smaller features such as the override and final (or
the non-standard sealed in Visual Studio) keywords and nullptr allow you to to
be clearer the intent of your code.For me, less visual clutter and being able to
express my intentions in code more clearly means I m happier and more
productive.
9. Error detection. If your errors happen at runtime, it means that at the
very least you need to run the software, and probably perform a series of steps
to reproduce the error. This takes time.C++11 provides a way to check
2. preconditions and catch errors at the earliest possible opportunity   during
compilation, before you even run the code, which is this is achieved by using
static_assert and the type traits templates. Another advantage of this approach
is that such checks don t incur any runtime overhead   one more point for
performance!
10. Strongly-typed enums "Traditional" enums in C++ have some drawbacks: they
export their enumerators in the surrounding scope (which can lead to name
collisions, if two different enums in the same have scope define enumerators
with the same name), they are implicitly converted to integral types and cannot
have a user-specified underlying type. These issues have been fixed in C++ 11
with the introduction of a new category of enums, called strongly-typed enums.
They are specified with the enum class keywords. They no longer export their
enumerators in the surrounding scope, are no longer implicitly converted to
integral types and can have a user-specified underlying type (a feature also
added for traditional enums).
enum class Options {None, One, All};
Options o = Options::All;