- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
这是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
. Thenullptr
keyword designates a constant rvalue of typedecltype(nullptr)
. We also provide thetypedef
:typedef decltype(nullptr) nullptr_t;
nullptr_t
is not a reserved word. It is atypedef
(as its_t
typedef
indicates) fordecltype(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/
我看到微软的 stddef.h 定义 nullptr_t 是这样的: namespace std { typedef decltype(__nullptr) nullptr_t; } usin
我无法从 C++11 标准中判断 nullptr_t 是否具有默认构造函数。换句话说,以下是否有效?: nullptr_t n; GCC 和 VC++ 允许使用上述代码,但 clang 不允许。我在标
当使用文字 0 时在 C++ 中,编译器无法区分指针和 nullptr_t函数的重载。 说明问题的代码: struct Bar {}; void foo(Bar*) { std::cout #i
第 4.10/1 N3797 节说: A null pointer constant is an integer literal (2.14.2) with value zero or a prval
我有以下带有自定义 Variant 类和自定义 SmartPtr 类的代码: using namespace std; class Object { public: }; template class
以下 C++11 程序在 gcc 4.7.2 下不输出任何内容: #include using namespace std; decltype(nullptr) g() { cout op
引用 C++11:(18.2/9) nullptr_t is defined as follows: namespace std { typedef decltype(nullptr) nullptr
这是nullptr_t的声明在 : namespace std { typedef decltype(nullptr) nullptr_t; } 根据 this , std::nullptr_t是
我了解到nullptr除了可以转换为任何指针类型(但不能转换为任何整数类型)之外,还有自己的类型std::nullptr_t。所以有可能有一个接受 std::nullptr_t 的方法重载。 究竟为什
今天才知道C++允许std::nullptr_t:类型的非类型模板参数 template struct A { }; template void f() { } 在我的一生中,我无法为这些提出任何明智
使用 C++20 的 concept s 我注意到 std::unique_ptr 似乎无法满足 std::equality_comparable_with 概念。从 std::unique_ptr
我正在为游戏制作组件实体系统。 我有一个类,Object,它只是一个用于容纳组件的外壳,这些组件又作为 (typeid, unique_ptr<>) 对存储在 std::map 中的对象中。每个组件都
最初该项目编译并运行良好。然后,在不修改 setuptab.h|.cpp 文件的情况下,我试图避免创建 setuptab 的新实例,所以我尝试将其传递过去。 错误 No matching constr
我有一个启用的模板函数(通过 std::enable_if),它的参数是一个原始指针,或者有一个 std::iterator 类别或者是一个 std::nullptr_t。在该函数中,原始指针(数据成
这个问题在这里已经有了答案: Why is std::is_assignable counter-intuitive? (3 个答案) 关闭 5 年前。 cout ::value ::value <
显然 std::nullptr_t 参数在没有参数传递时被转换为 void * 类型的空指针(N3337 的第 5.2.2/7 节) (通过 ...)。这意味着要正确传递空 char * 指针,例如,
长话短说: 为什么模板化函数不能访问非模板化函数可以访问的相同转换? struct A { A(std::nullptr_t) {} }; template A makeA(T&& arg)
考虑以下代码: #include using namespace std; void fun(const char* s){ if (s == nullptr) { puts
这段代码: #include #include using namespace std; void dump(const std::string& s) { cout &) {
有人正在编译我使用 C++11 的 Qt 程序标准,他们得到了这个错误(Mac OS X/gcc)。我知道我可以声明它,但它不应该已经在 中了吗? ? ./collectable_smartptr.
我是一名优秀的程序员,十分优秀!