The C++ nullptr is of the type std::nullptr_t.
Why does a program like
int main() {
int* ptr = nullptr;
}
still work, although it doesn't include any STL library?
c++c++11nullptrpointersstl
The C++ nullptr is of the type std::nullptr_t.
Why does a program like
int main() {
int* ptr = nullptr;
}
still work, although it doesn't include any STL library?
Best Answer
In C++11 they wanted to add a keyword to replace the macro
NULL
(which is basically defined as#define NULL 0
), both because it is a core concept, and because of some annoying bugs you get when you are forced to use0
as your null pointer constant.A number of keywords where proposed. Large codebases where searched to ensure that the keyword was not in use, and that it still described what they wanted (a null pointer constant).
nullptr
was found to be sufficiently rare and evocative enough.The type of
nullptr
was not given a keyword name by default, because that wasn't required for most programs. You can get it viadecltype(nullptr)
or including astd
header and usingstd::nullptr_t
.