Why can I use nullptr without including STL?

与世无争的帅哥 提交于 2019-12-06 20:24:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!