The old NULL was a define of the value 0, and this is can generate some problems and bugs.
Here is one of the problem that NULL can give you. In this example we got 2 overloads and we want the pointer overload to be called. But if we send NULL to it. We will not get the expected behavior.
void method(int *p); void method(int n); method(NULL); // what overload is going to be used. ?? // method(int n); will be called.
By using nullptr that problem will be avoided.
void method(int *p); void method(int n); method(nullptr); // method(int* p); will be called.
This may be a minor problem. The more serious problem happens when you write templates, and they can also produce very strange compiler error that can be hard to figure out since the compiler will complain about something else. Specially if you are also using some of the new C++11 features like auto, rvalue references, shared_ptr. Then you might get really weird compiler error if you use NULL, instead of nullptr.
So start to use nullptr instead of NULL. Better code, less risk for bugs.
More Information
Stephan T. Lavavej: Everything you ever wanted to know about nullptr
WikiBooks - C++ Idioms/nullptr
No comments:
Post a Comment