Programming in modern C++ with C++ 11

If you are following C++ lately you might have herd of C++ 11. C++ 11 is new revision to C++ standard approved in 2011. It offers significant improvements in both the language and the library. It include features such lambda functions/expression, range based for loop, uniform initialization, automatic type deduction, variadic templates and many more.
On the library side changes include regular expressions, atomic operations, threading (futures and promises), and tuples. Even though some of these feature exits for while in boost library now it is part of C++ standard hence it will be available from all compiler vendors (GCC,VC++ and CLANG).

With these improvements , programming in C++ 11 is as easy as in Java or C#.

In this blog post I will cover some of features I have explored in C++11.

1. Automatic Type deduction with ‘auto’

With C++ 11 , you can declare variable or object without specifying its specific type by using ‘auto’ keyword. This is especially useful if you are working with templates or lambda functions where actual type can be hard to write but compiler can infer from the expression. This feature is similar to c# var keyword

auto count  = 10; // compiler infer count type has int

vector wordCount = { 10,20,30,40 };

auto pos = wordCount.begin() ; //  compiler infer pos has vector::iteraror type.

// compiler infer the squareFunction is lambda function with integer input and integer return type.
auto squareFunction = [] (int x) -> int
                      { return x*x; };

// other example is iterating over any C++ containers using ranged based for loop
for(auto wordC : wordCount )
{
   cout<<wordC<<endl;

   cout<<squareFunction(wordC)<<endl;
}

2. Uniform initialization and Initializer list

This feature allows initialize any type (custom or built-in) using single common syntax. Following examples shows initializing built-in primitive types, containers and custom object using braces syntax

int count = { 10 };

double tempratureRange []  = {32.7,46.9,40.6,67.1,12.8 };

vector days = { "sunday","monday","tuesday",
                        "wednesday","thursday","friday","saturday"
                      };
map<string,vector> phoneList = { {"Joe",{"222-456-234","222-111-555"}},
                                         {"John",{"444-656-345","111-123-567"}}
                                        } ;
CustomType ct = {1,2,3,4,5,6};

for(auto day : days )
{
   cout<<day<<endl;
}

for(auto & phone : phoneList )
{
  cout<<phone.first<<endl;
  for(auto & num : phone.second )
       cout<<num<<" , ";
  cout<<endl;
}

3. Lambda functions/expression

This feature allows creating anonymous functions that can be passed/returned from another function. This feature used along with C++ generic function/algorithms allows writing short snippet of code inline to perform operation on C++ containers.

[] {
     cout<<"Hello from lambda"<<endl; }(); // square function auto squareFunction = [] (int x) -> int
                         { return x*x; };

vector values = {1,2,3,4,5};

for_each(values.begin(),values.end() , [&values] ( int n ) {
                                       cout<<n;
                                      if ( n % 2 == 0 )
                                         cout<<" is even number"<<endl;
                                      else
                                         cout<<" is odd numnber"<<endl;
        });

4. Range based for loops

This feature makes iterating over collection using simple syntax. It works with all standard containers

vector days = { "sunday","monday","tuesday",
                        "wednesday","thursday","friday",
                        "saturday"};
map<string,vector> phoneList = { {"Joe",{"222-456-234","222-111-555"}},
                                         {"John",{"444-656-345","111-123-567"}}
                                        } ;
// iterating over vector
for(auto day : days )
{
   cout<<day<<endl;
}

// iterating over map
for(auto & phone : phoneList )
{
    cout<<phone.first<<endl;
    for(auto & num : phone.second )
        cout<<num<<" , ";
    cout<<endl;
}

5. Variadic templates.

This feature allows users to write template class or function that can take variable number of types. For example writing type safe printf style function: See Wikipedia article . This feature will be more useful for library authors. Tuples are implemented using variadic templates as tuple can take multiple arguments of various types.

6. Tuples and Raw string literals

Tuples are ordered list of elements, many of the functional programming languages have this feature. Basically it allows to pass/return multiple values to/from function as single unit without creating separate custom type to do the same.

Example: to parse tcp host details to separate hostname and port.

tuple<string,string> ParseTcpHostDetails(const string & tcpHost)
{
   string host = tcpHost.substr(0,tcpHost.find_first_of(':'));
   string port = tcpHost.substr(tcpHost.find_first_of(':')+1);
   return make_tuple(host,port);
}
int main(int argc,char* argv[])
{
   auto parsedValue = ParseTcpHostDetails("127.0.0.1:9090");
   string host = std::get(parsedValue);
   string port = std::get(parsedValue);
}

Raw string literals allows to write character strings where escape character are not processed. A raw string literal starts with R”( and ends in )”

string uncPath = "\\\\servername\\sharepath";
string uncPathWithRawStringLiterals = R"(\\servername\sharepath)";

7.  Threading support ( async,futures and promises )

These features collectively help in writing concurrent application in a standard way on different OS platforms.

async allows launching task that can be executed on background thread, future allows waiting for the task to complete and wait for the result

int main(int argc ,char* arv[])
{
  std::future result1(std::async( [] () {
     cout<<"["<<std::this_thread::get_id()<<"] Inside long running task .. "<<endl;
  }));

  cout<<result1.get()<<endl;
}

All samples programs are compiled on Ubuntu 12.10 with GCC 4.7.2 compiler with -std=c++0x switch.

// sample c++ file for compilation with proper header include
// use 'g++ -g -std=c++0x -o Sample Sample.cxx' compiler options
#include
#include
#include
<map>
#include
#include
#include
using namespace std;
int main(int argc,char * argv[])
{

return 0;
}

In future blog posts I will write about remaining C++ 11 features such as

  • Decltypes
  • Move constructor & Delegating Constructors
  • Move semantics
  • New ways of making a Class non copy able and assignable using keyword
  • New smart pointer classes
  • More thread/async/future/promise examples

Resources

1. isocpp website provides all the news , articles ,videos and blogs related to C++.

2. C++ 11 FAQ by Bjarne Stroustrup.

3. podcast :Excellent C++ 11 podcast on software engineering radio with Scott Meyers.

4. Future of C++ : Build 2012 Talk by Herb Sutter.

Advertisements