Tuesday, 9 April 2013

C++ 11 ( some important features ) Part2


Hello friends,
            Welcome you all in the C++ 11 Part2. In the Part1 we have discussed about auto and decltype.
In this part we will learn about nullptr and enum classes.


Let’s start with nullptr.

nullptr :-
            nullptr has been introduced in C++ 11. nullptr denotes the null pointer literal.
            nullptr is a of type nullptr_t, which is implicitly convertible and comparable to any pointer type and any pointer to member type.
           
            Let's see this :-

                void fun1(char *i);
                void fun1(int j);
           
                fun1( NULL );           // which function gets called ?
           
            It looks like the first function will be called.
            The trouble is value of NULL is integer 0, so the second fun1(int j) will be called.
           
            With the help of nullptr we can solve this very easily.
           
            fun1 ( nullptr );
            Now the first function fun1(char *i) will be called. nullptr is used to represent NULL pointers,
            and cannot be converted to an integer.
           
enum classes:-

            We have all used enums. Can you tell me is it type safe?

            enums in C++03 are not type safe. They are treated like as integer constant even if the enumeration types are distinct.

            Let see the example:-
#include < iostream>
using namespace std;
 
int main()
{
    enum Song
    {
        track1,
        track2
    };
 
    enum Video
    {
        video1,
        video2
    };
 
    Song s = track1;
    Video v = video1;
  
    if (s == v) // The compiler will compare a and b as integers
        cout << "s and v are equal, no type safe" << endl; // and find they are equal!
    else
        cout << "s and v are not equal, type safe" << endl;
  
    return 0;
}       
  
                    
            C++11 has introduced enum class, which makes enums type safe and strongly scoped.
            Lets take the same example with use of enum class.
           
#include < iostream>
using namespace std;
  
int main()
{
    enum class Song
    {
        track1,
        track2
    };
  
    enum class Video
    {
        video1,
        video2
    };
  
    Song s = Song::track1; // track1 is not accessible, we have to use :: operator
    Video v = Video::video1; // track1 is not accessible, we have to use :: operator
     
    if (s == v) // compile error here, as the compiler doesn't know how to compare different types Song and Video
        cout << "s and v are equal, no type safe" << endl; 
    else
        cout << "s and v are not equal, type safe" << endl;
  
    return 0;
}
         
            
            enum class makes the enums strongly scoped. If you want to access it you have to use :: operator.
            enum class makes the enums type safe. So C++ will look for an explicilty defined comparison function to compare Song and Video. Because we have not defined an operator==(Song, Video) function the compiler won't understand how to compare s and v. That is why it is giving compiler error.