本文介绍了为什么在不包含STL的情况下可以使用nullptr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ nullptr 的类型为 std :: nullptr_t .

The C++ nullptr is of the type std::nullptr_t.

为什么像

int main() {
 int* ptr = nullptr;
}

尽管它不包含任何STL库,但仍然可以正常工作吗?

still work, although it doesn't include any STL library?

推荐答案

在C ++ 11中,他们想添加一个关键字来替换宏NULL(基本上定义为#define NULL 0),两者都因为一个核心概念,并且由于某些烦人的错误而使您不得不使用0作为空指针常量.

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非常稀有且令人回味.

nullptr was found to be sufficiently rare and evocative enough.

nullptr的类型在默认情况下未指定关键字名称,因为大多数程序不需要此名称.您可以通过decltype(nullptr)或包含std标头并使用std::nullptr_t来获取它.

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.

这篇关于为什么在不包含STL的情况下可以使用nullptr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 22:00