#include <iostream>
#include <string>
int main() {
std::pair<std::string, int> s;
std::cout << s.second << std::endl;
}
在这个例子中 s.second
是 0
虽然它没有被初始化。您能否提供指向 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; [...]