“C++11 feels like a new language.” - Bjarne Stroustrup
C++11 provide many useful features to boost programmers’ productivity dramatically. Below I listed some ‘must-know’ features:
autoto avoid redundantly restate a type’s name. Note that using
autodoesn’t mean your code becomes dynamically-typed, it is still statically typed and the code meaning stays the same. Use auto whenever possible to save you time.
Always use the standard smart pointers and remove owning raw pointers and delete! There are three standard pointers:
weak_ptr. Here is some explanations about how to use them and the [wiki page] about smart pointers.
nullptrfor null pointer value, never use literal 0 or NULL macro because they could both mean an integer and a pointer.
Range-based for loop - finally a for loop just like the ones in scripting languages:
Lambda functions. Yes, the lambda functions you want! Just like the lambda function in Python or other popular languages, it is basically an unnamed temporary local function that can be created with ease. Now forget about functor objects!
rvaluereference (&&) and
movesemantics. Since C++11,
rvaluereference is introduced, which makes move semantics possible. So if you want your own data structure to take advantages of move semantics, make sure move constructor and move assignment operator is implemented for your own class. And the good news is, move semantics has been added to all the standard library objects (vectors, maps, strings, etc.), that means using this feature on the STL objects will just work! From now on, you can do return by value for large objects a lot more often and not worry about it slowing down program performance!
Uniform initialization and initializer lists - this avoids declaring a function rather than a variable intialization.
C++03 to me is not like an improved C, but a more complex C, which makes writing C++ more error-prone and more complicated. But with the new features introduced in C++11, it is indeed more like a new language, which makes me start to like writing C++11 programs.