int a=int(); C++98 中会发生什么?

int a=int(); what happens in C++98?

在您考虑将问题标记为重复之前,请完整阅读问题。像

这样的语句

int i=int();

大多数程序员会说这里有值初始化&i会进行值初始化。 (0 作为输出)。但它也在 C++98 编译器上打印 0 作为输出。 以下程序是我在 C++98 实现上测试的,输出为 0。

#include <iostream>
int main()
{
     int i=int();
     std::cout<<i;
}

在C++98以上的程序中不要说i是值初始化,因为C++03引入了值初始化。那么我是如何在这里初始化的呢?它真的是构造函数调用吗? int() 看起来像构造函数调用。正如 Bjarne stroustrup 在他的书 C++ 编程语言和 TC++PL 中所说的那样,原始类型在 C++ 中也有默认构造函数。

C++ 编程语言 Bjarne stroustrup:

10.4.2 Built in types also have default constructors

还阅读了同一本书的第 6.2.8 节。

以下链接还说明内置类型在 C++ 中具有默认构造函数。

1) http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=15

2) http://www.geeksforgeeks.org/c-default-constructor-built-in-types/

那么我真的可以说它是整数类型的构造函数调用吗?

5.2.3 Explicit type conversion (functional notation)

2 The expression T(), where T is a simple-type-specifier (7.1.5.2) for a non-array complete object type or the (possibly cv-qualified) void type, creates an rvalue of the specified type, whose value is determined by default-initialization (8.5; no initialization is done for the void() case). [...]

8.5 Initializers

5 [...] To default-initialize an object of type T means:

-- if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

-- if T is an array type, each element is default-initialized;

-- otherwise, the storage for the object is zero-initialized.

没有问题。 int() 从第一个 C++ 标准开始就保证求值为零。它是通过默认初始化而不是值初始化发生的,这是一个与您的问题完全无关的技术细节。