Robert Hönig Robert Hönig - 1 year ago 81
C++ Question

Why can I use nullptr without including STL?

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download