Tuesday, February 3, 2015

Practical Applications of C++11 and Why You Should Use It

A couple years ago I began looking at the experimental support of some of the newer C++11 features. At the time, compilers only offered subsets of the functionality. My first exposure was the 'auto' keyword. I didn't like. I want a strongly typed language when I use C++. `auto` seemed so lazy and sooo un-explicit. After listening to several cppcon talks on youtube, including one by Herb Sutter, I realized that auto can be a useful tool when used appropriately.

My experience with `auto` illustrates one of the major barriers for established C++ developers using newer C++ features. I don't know if you know this, but C++ has been around for a long time. And by long time, I mean it is ancient as far as languages are concerned. Sure, it is newer than Fortran and COBOL, but at the rate at which programming languages have exploded, C++ is a grandparent with many grandchildren. Many senior developers have spent their entire careers developing in C++ with minimal change. Sure, they lived through the wild west of the STL. Sometimes they had to use assembly when C++ just wasn't giving them the performance they needed. C++ finally provided a language that was good enough to solve most programming problems. C++ accumulated significant technical debt when compared to other more modern languages. As such, when it came time, releasing a new standard took a long time and had snowballed into a huge feature set.

Fast forward to today. Several compilers actually comply to the full standard today, and will comply to future standards within months of new standards being released. How amazing is that? That means that you can use these awesome features without worrying about library compatibility or linking in 3rd party libraries. Plus, the built in features mean more cross platform compatible primitives. That... is huge.

Ok, so a cross platform range based for loop isn't a big deal, how about cross platform threads? Some of the new planned features may include networking, 2d graphics. How about web services? I'm betting that some of these planned features will rely heavily on the C++11 features. If you want these new features, you'd better start learning the foundation. So, a promise of new features sometime down the line is not really a good reason to learn the new standards. Let me move to my main point

C++11 features will make your life easier by reducing complexity, eliminating boiler plate code, and creating easily maintained code

Use these features. Please please use them

In the following posts I will go through some of the new features and actually talk about real world situations where these features will improve your code.

C++11 Features that I will cover with practical applications

  • auto
  • Range based for loops
  • Lambda functions and std::function
  • C++11 additions to std::algorithm
  • std::thread (and this_thread)
  • mutex, condition_variable, and locks
  • Smart pointers
  • Variadic templates
  • std::chrono
  • And other miscellaneous goodies...