Learning New C++ Standards

| | 2 min read

For a long time I used to develop and maintained C++ applications. Sure the version is 98. But lot of things are happening in C++ community that I was not aware of. Now I got some free time I noticed those changes. The big change was a set of new standards are got approved(C++11. C++14) and few standards are waiting for approval.

The significant milestone was the introduction C++ 11 which is ISO C++ 2011. So I decided to learn that standard. Some says its like a new language. Lets take a tour on how it look like.

Compiler support

Gnu C++ compiler GCC is the first compiler bring new standard features to C++ developer community. With GCC 4.3 and later we can use those features. I'm using 4.6.3. To enable it, use -std=gnu++0x flaf to the g++ command line. For GCC 4.7 and later use -std=c++11 and -std=gnu++11 as well.

Installing gcc 4.9 on Ubuntu 12.04

Use following commands to install gcc 4.9 one Ubuntu 12.04.

 sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.9
sudo apt-get install g++-4.9 

Now compiler is ready. Lets eat the elephant one bite at a time.

auto specifier

Instead of specifying the type of variable at the time of declaration we can now use auto keyword. The type of the variable is automatically find out from the initializer.

 auto number = 10;
auto circle2 = circle1; 

Similarly function return type can also be auto.

 auto add(auto arg1, auto arg2)
  {
    return arg1 + arg2;
  } 

auto keyword simplifies the variable declarations. So now no need to write long declarations like
map<long,list<long>>::iterator iter . Simply auto iteration is enough.