A foolish consistency is the hobgoblin of little minds, adored by little statesmen and philosophers and divines - Self-Reliance, Ralph Waldo Emerson
What's New in C++11
I heard a lot of cool things about the new features and here is what I came across so far:
New Keywords
“long long”
C++11 defines a new integer type “long long”, which is guaranteed to be at least 64 bits in length. The largest integer type “long” defined in C++03 is platform specific, so can be 32 or 64 bits in length. Just a heads up, C99 and other compliers already supported it even before C++11.
“auto”
Automatic type inteference has been introduced using “auto” keyword.
autoa=10;// a is infered to be of type intfloatb=20.5;// b is infered to be of type floatintc=a;// c is infered to be of type intstrings="test";// s is infered to be of type const char *vector<int>v;v.push_back(1);v.push_back(2);autod=v[0];// d is infered to be of type intfor(autoitr=v.begin();itr!=v.end();itr++){...}// itr is infered to be of type vector<int>::iteratorfor(autoitr:v){cout<<itr;}// Another easier way to do the same, called range-based // for statement. itr is read only herefor(auto&itr:v){cout<<++itr;}// Another easier way to do the same, called range-based // for statement. itr is writable here
“decltype”
To know the type of an expression at the compile time. For eg,
New in C++11
12345
decltype(a)a2;// a2's type is int since a is of type int (above)constint&&fun();decltype(fun())e;// e's type is const int&&decltype(b)b2=0.5;// b2's type is float since b is of type float (above)
“nullptr”
No more initializing pointers with NULL which can both act as an int or int * leading to oddity.
New in C++11
123
int*f=nullptr;autog=NULL;
“enum class”
enums have got an upgrade too and now they are type safe
Previously in C++03
1234
enumSPORTS{BADMINTON,TENNIS};enumPERSON{EMILY,BOB};if(BADMINTON==EMILY){}// if condition will evaluate to true
New in C++11
1234
enumclassSPORTS{BADMINTON,TENNIS};enumclassPERSON{EMILY,BOB};if(SPORT::BADMINTON==PERSON::EMILY){}// if condition will evaluate to false
“static_assert”
C++11 defines a new compile time assert.
New in C++11
12
static_assert(sizeof(int)==4,"This program cannot be run on machines not supporting integer to be 4 bytes long");
“default”, “delete”, “final” and “override”
All of these 4 new additions are w.r.t. to the inheritance constructs:
“default”
To explicity tell the compiler to provide the default constructor. The only use I see is the enhanced readability.
New in C++11
123456
classTemp{public:Temp(){}// Own implementation of the default constructorTemp()=default;// Explicitly ask compiler to provide the implementation};
“delete”
To not to allow anyone call a function. It is definitely very useful.
New in C++11
1234567
classTemp{public:Temp(){}Temp(constTemp&)=delete;// Does not allow copy constructionTemp&Temp(constTemp&)=delete;// Does not allow assignment operation};
“final”
To not to allow anyone override a virtual function or inherit from class. Looks kind of odd :)
New in C++11
12345678910111213
classB{public:virtualf()final;// Overriding not allowed};classD{public:virtualf();// Compiler error};classB2final{};// Inheriting not allowedclassD2:publicB2{};// Compiler error
“override”
To explicity tag functions we intend to override in a derived class class. Definitely useful.
C++11 patches up the inconsistency between initializing simple data types and containers:
Previously in C++03
12345
intv[3]={0,1,2};structPERSON{intid;stringname};PERSONp={1,"EMILY"};vector<int>v2[3]={0,1,2};// Not possible in C++03
New in C++11
12
vector<int>v2[3]={0,1,2};// Possible in C++11
The way it works is that it calls the constructor std::vector(std::initializer_list). std::initializer_list is a new template class. So, you can add this into your container classes as well:
vector<int>v(3);// Creates a vector of size 3 with uninitialized elementsvector<int>v{3,2,1};// Creates a vector of size 3 with elements 3, 2, 1. This is called uniform initialization
This concept can be used during the function calls and while returning from the functions too:
It is now possible to call an another constructor from the first one i.e we can chain constructors:
New in C++11
12345678910111213
classMyVector{public:MyVector(){// accomplishes task A}MyVector(inta):MyVector(){// accomplishes task A and B}MyVector(inta,intb):MyVector(a){// accomplishes task A, B and C}};
Template Alias or Type Alias
Just like typedef, you can create template alias or type alias (synonyms) refer to a familty of template types. Then, what is the difference between typedef and type alias. Here, it is:
Previously in C++03
123456789
template<classT>classMyVec{};typedefMyVec<int>V1;V1a1;// Okaytypedeftemplate<classT>classMyVecV2;// Compiler error heretypedeftemplate<classT>MyVecV2;// Compiler error here// V2<int> a2; // Not possible. So, typedefs cannot be templated
As you can see, the limitation with the typedef is that they cannot be templated. This is where type alias comes in picture: