RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


nullptr: A Type-safe and Clear-Cut Null Pointer-3 : Page 3




Building the Right Environment to Support AI, Machine Learning and Deep Learning

nullptr is a reserved keyword designating an rvalue constant of type std::nullptr_t. The underlying type of std::nullptr_t is implementation-defined. However, it must be a POD type, whose size is the same as sizeof(void*). nullptr is implicitly converted to pointers and pointers to members. However, nullptr is not convertible to bool or any of the numeric types of C++:

//pointers and numeric types char *pc=nullptr; //OK char c=nullptr; //error, type mismatch int num=nullptr; //error, type mismatch //pointers to members int (A::*pmf)(char *)=nullptr; //OK int A::*pmi=nullptr; //OK, pointer to data member //comparisons if (nullptr==0) //error, type mismatch if (nullptr)//error, conversion to bool not allowed if (pc==nullptr) //OK

See how nullptr elegantly solves the overload resolution problem shown earlier:

void func(int); void func(char *); int main() { func(nullptr); //calls func (char*) func(0); //calls func(int) }

nullptr can be used in a sizeof expression. Additionally, you can use nullptr as the argument of a typeid expression:

cout<< typeid(nullptr).name(); //OK size_t n= sizeof(nullptr); //n=sizeof(void *).

Throwing and copying nullptr_t objects is also allowed. In the following code listing, the throw statement inside the try block throws nullptr. The matching catch block catches this object by value:

try { throw nullptr; //OK } catch(std::nullptr_t np) {}

Notice that you cannot take the address of nullptr, though it is possible to take the address of another object of type std::nullptr_t:

template <typename T> void func(T) {/**/} func (&nullptr); //error, can't take nullptr's address std::nullptr_t nullp; func(&nullp);//OK

Future Directions
The latest draft of the nullptr proposal is available here. Although C++ compilers don't support this feature yet, you can use macros to ensure smooth migration to nulltpr in the near future:

#ifndef __CPP09NULLPTRSUPPORTED #define NULLPTR (0) #else #define NULLPTR (nullptr) #endif

C++09 will retain compatibility with older versions of C++. Therefore, 0 will remain in use as a null pointer constant in C++09, in addition to the normative nullptr:

char *pc=0; //OK, pc is a null pointer even in C++09 if(ch1==nullptr) //OK

Similarly, the current definition of the macro NULL will not change in C++09.

Danny Kalev is a certified system analyst and software engineer specializing in C++. He was a member of the C++ standards committee between 1997 and 2000 and has since been involved informally in the C++0x standardization process. He is the author of "The ANSI/ISO Professional C++ Programmer's Handbook" and "The Informit C++ Reference Guide: Techniques, Insight, and Practical Advice on C++."
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date