Introduction to C++ 11 Series – Part 10, Ranged-Based for-Loops

April 9, 2013

Yes, this is post #10 in the C++ 11 series. The last post was long and complicated. In this post, I would like to write about something simpler - Ranged base loops. Ranged Based Loops, are like the C# foreach loop. The idea is to simplify iteration syntax on containers that have a beginning, an ending, and a forward iterator. Suppose we have this vector: vector<int> v; for (int i = 1; i <= 10; ++i)        v.push_back(i);   To iterate through the vector elements, use: for (auto i: v)       ...

Introduction to C++ 11 Series – Part 9, R-Value, L-Value, Move Semantics and Perfect Forwarding

April 3, 2013

Take a deep breath; this is a long post about one of the most important and most complex features of C++ 11. One of the most performance-boost features of C++ 11 is the new move semantics addition to the language. This feature is based on a new reference – an R-Value reference. In this post, I am going to explain what R-Value (and L-Value) is, why we need it, and how we can use it to enable move constructor & move assignment operator. We will also see the std::move function and talk about perfect forwarding and the std::forward...

Introduction to C++ 11 Series – Part 8, Lambda Expression or Functions

April 1, 2013

One of the most important features of C++ 11 is the addition of Lambda, an inline function mechanism (and a closure). Motivation The first motivation is to make this a valid C++ code: (){}(); There are many reasons to use lambda and functional programing; for example, STL algorithms use function or functor objects. STL <functional> provides a set of functors such as plus, minus, less, equal_to, etc. Most of these functors are binary functions, i.e. they take two parameters. To use a predefined binary functor in an STL algorithm, you need to...