nullptr
suggest changeA keyword denoting a null pointer constant. It can be converted to any pointer or pointer-to-member type, yielding a null pointer of the resulting type.
Widget* p = new Widget();
delete p;
p = nullptr; // set the pointer to null after deletion
Note that nullptr is not itself a pointer. The type of nullptr is a fundamental type known as std::nullptr_t.
#include <cstddef>
void f(int* p);
template <class T>
void g(T* p);
void h(std::nullptr_t p);
int main() {
f(nullptr); // ok
g(nullptr); // Error: T cannot be deduced from std::nullptr_t.
h(nullptr); // ok
}
Template argument deduction cannot infer T for g(nullptr) because nullptr has type std::nullptr_t, not a pointer type. An explicit argument, such as g<int>(nullptr), would compile.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents