问题
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?
回答1:
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 use 0
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 via decltype(nullptr)
or including a std
header and using std::nullptr_t
.
来源:https://stackoverflow.com/questions/39080709/why-can-i-use-nullptr-without-including-stl