c++ - int() 为 0 的定义在哪里?

标签 c++ standards

<分区>

#include <iostream>
#include <string>

int main() {
    std::pair<std::string, int> s;
    std::cout << s.second << std::endl;
}

在这个例子中 s.second0 虽然它没有被初始化。您能否提供指向 C++ 标准的链接,其中定义了为什么是 0。我知道这是因为 s.second 是由 int() 初始化的,但是找不到标准中声明 int() 的行是 0

最佳答案

8.5 初始化器 [dcl.init]

10) An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

7) To value-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T’s implicitly-declared default constructor is non-trivial, that constructor is called.
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized.

我猜

5) To zero-initialize an object or reference of type T means:
— if T is a scalar type (3.9), the object is set to the value 0 (zero), taken as an integral constant expression, converted to T; [...]

关于c++ - int() 为 0 的定义在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15548813/

相关文章:

c# - 混合 C#、托管 C++ 和非托管 C++ 应用程序的日志记录框架

java - 在 Windows 上哪里可以找到 java 应用程序的用户配置文件?

javascript - 为什么支持非js

c - 为空结构启用警告

c++ - 未使用的函数能否根据 C++14 实例化具有副作用的变量模板?

C++ 头文件,foo.h 与 cfoo

c++ - 如何将绘制的点保存在列表中的 QWidget 上?

c++ - 如何获得指针的头部?

c++ STL cout源代码

c++ - 将函数传递给模板时,使用函数指针或对象有什么区别?