c++ - 局部作用域变量是初始化为未确定的值,还是未初始化?

标签 c++ language-lawyer

迂腐地说,x是否在下面的代码中初始化了?

int main()
{
    int x;
}

8.5 Initializers [dcl.init](对于 C++11)中有一些关于它的段落,但没有任何示例支持。

最佳答案

它是正式的默认初始化,这意味着对于int,不执行任何初始化。

[dcl.init]/12 (N3797)

If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value

[dcl.init]/7

To default-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type, the default constructor for T is called [...];

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

  • otherwise, no initialization is performed.

关于c++ - 局部作用域变量是初始化为未确定的值,还是未初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25999309/

相关文章:

c++ - 使用C时间函数测量时间: are they code-reordering resistant?

c++ - 显式程序在 msvc 中有效,但在 gcc 中无效

c++ - 具有更多参数的信号处理程序

c++ - 可变参数模板的英特尔编译器错误评论?

c++ - 虚拟非方法成员

c++ - MediaCapture如何使用录音

c++ - 是否允许用其他类型替换 `this`?

c++ - 返回对/结构/元组时参数的顺序在下面的代码中重要吗?

c++ - friend 、私有(private)函数、模板别名和 decltype ......在拒绝这个方面是正确的吗?

java - 在移植用 libsndfile 编写的 C++ 代码时,我应该在 Android 中使用什么?