You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It doesn't have a pointer type, but you can think of it as a pointer of all types.
nullptr's actual type is std::nullptr_t, which defined as typedef decltype(nullptr) nullptr_t;(see std::nullptr_t).
The type std::nullptr_timplicitly converts to all raw pointer types, and that's what makes nullptr act as if it were a pointer of all types.
Advantages
Using nullptr instead of 0 or NULL thus avoids overload resolution surprises.
// three overloads of `f`voidf(int);
voidf(bool);
voidf(void*);
f(0); // calls f(int)f(NULL); // might not compile, but typically calls f(int). Never calls f(void*)f(nullptr); // calls f(void*), i.e. only nullptr works
Improves code clarity, espically when auto variables are involved.
auto result = findRecord( /* arguments */ );
// type of result may not be clearif (result == 0) { // can not know result is integer or pointer// ...
}
// no ambiguityif (result == nullptr){ // result must be a pointer// ...
}