C++ 11's new style, explained by Bjarne Stroustrup

In a series of very interesting presentations in Microsoft's GoingNative2012 event, C++'s main creator, Bjarne Stroustrup himself, talked about the programming style introduced by the brand new C++11 standard.

The main focus of Bjarne's presentation was not the new language features, but how to use them correctly; that's what he calls "style". Up next, a summary of the key points raised:

  • Type-rich interfaces: Programmer-defined suffixes can now be used to indicate measuring units (see the new constexpr keyword).
  • Use RAII to handle resources: memory, files, sockets, hardware, operating system resources (threads, mutexes, semaphores, etc).
  • Avoid raw pointer usage, specially in public interfaces (restrict those usages to single method implementation scope). Use smart pointers classes such as unique_ptr and shared_ptr. Don't use the latter if the resource needs to be shared. And before considering pointers, consider references, or even better, create objetos on stack. All this discussion pertains to using pointers to express possession of a resource. If we just want to point to an object which does not belong to us and we can't use a reference, a pointer is acceptable.
  • Avoid native arrays; use STL containers. If passing a native array to a third-party API is necessary, an std::vector can be used, since it is guaranteed by the standard to use contiguous memory, just like a native array does. The API function should then be passed &v[0], where v is an std::vector.
  • When managing large objects (for instance, a Matrix class which we use to represent 10000 x 10000 matrices), and we want to write a method which returns an instance of that class (say, an operator+), we are forced to create a local instance and return a copy. That's a lot of overhead; a new, essential feature of C++11 is "move semantics". Instead of copying, we "move". In the previous example, we return a Matrix by value and implement a "move constructor", which defines how the "move" operation should be performed. Details on this will be left to future posts. Suffice it to say that all STL containers implement this, with the exception of std::array. This implies a significant performance boost just by compiling existing STL-using code with a C++11 compiler.
  • Use whenever possible.
  • Low level code isn't always more efficient. (for instance: C's sort function is orders of mangnitude slower than C++'s qsort). Always measure (profile) when in doubt and avoid complex premature optimizations. Low level usually means more code, which increases the chance of bugs and manteinance cost.
  • C++ 11 offers new classes for handling concurrency: std::thread and std::mutex, among others. Even better: std::async lets the runtime decide whether to use a separate thread or not, letting the programmer forget about many complexities of concurrence handling involved when handling theads manually.
  • También se agrega soporte para expresiones lambda.

Finally, here's a chart that lists which C++ 11 features are implemented by which compiler (MSVC = Visual Studio).

Comments

Popular posts from this blog

VB.NET: Raise base class events from a derived class

Apache Kafka - I - High level architecture and concepts

Upgrading Lodash from 3.x to 4.x