gpt4 book ai didi

c++ - 为什么 nullptr_t 不是关键字

转载 作者:可可西里 更新时间:2023-11-01 15:07:21 27 4
gpt4 key购买 nike

这是nullptr_t的声明在<cstddef> :

namespace std {
typedef decltype(nullptr) nullptr_t;
}

根据 this , std::nullptr_t是一些未指定的基本类型的别名,其中 nullptr是一个实例。所以 nullptr 的实际类型没有名字(好吧,语言没有给它起名字,这个名字是由标准库给出的)。

nullptr本身就是一个关键字。但是标准没有为 nullptr 的类型引入关键字。 .而是使用 decltype(nullptr)提供。

这样做的原因是什么?我发现它很困惑。您需要包含 header 并指定 std::仅使用语言内置功能。<​​/p>

这是为了让 C++ 关键字集尽可能小吗?这是专门针对 nullptr 的吗或者委员会将像这样声明所有新类型,所以我们将有 namespace std { typedef decltype(false) bool; }如果更早做出这样的决定?

最佳答案

根据nullptr的初步建议, N2431 (强调我的):

We propose a new standard reserved word nullptr. The nullptr keyword designates a constant rvalue of type decltype(nullptr). We also provide the typedef: typedef decltype(nullptr) nullptr_t; nullptr_t is not a reserved word. It is a typedef (as its _t typedef indicates) for decltype(nullptr) defined in <cstddef>. We do not expect to see much direct use of nullptr_t in real programs.

通常,委员会不愿意在语言中添加新的关键字。在我看来,这是有充分理由的,即向后兼容性。

如果您进一步阅读该提案,您会意识到主要问题不是破坏现有代码。

想象一下,如果委员会时不时地引入一个新关键字会产生什么影响。一切都会崩溃,成功故事中的 C++ 将成为一个大笑话。

关于c++ - 为什么 nullptr_t 不是关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34107491/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com