Range based for loop of C++11 Similar to 'for each'
June 25, 2012
Lambda functions and expressions in C++11
June 27, 2012

Separate nullptr in C++11

In my earlier articles on C++, I have written about the automatic type detection, uniform initialization list and Range based for loopin C++11.
Earlier we used to use the NULL macro which is synonymous to int zero. This came as an inheritance from C language where 0 plays a double role (as an int and as NULL pointer).
NULL pointer in C language is defined as either ((void *) 0 ) or 0 (int), but in C++ it is defined as int 0 for most of the cases.
If NULL is defined as 0 (int zero), and there are two overloaded functions:

   void fun(int);
   void fun(int*);

Now function call

    fun( NULL );

will call fun(int)which may not be the intention.
To overcome this ambiguity C++ introduced new, strongly-typed keyword nullptrnullptr is applicable to all pointer categories including function pointers and pointer to class members.

    fun(0);        // Call fun(int)
    fun(nullptr);  // call fun( int* )

 
Previous: Range Based for loop similar to for-each

Leave a Reply

Your email address will not be published. Required fields are marked *