c++ - 值初始化 : default initialization or zero initialization?

标签 c++ constructor initialization language-lawyer c++14

我已经模板化了 gray_code 类,该类旨在存储一些无符号整数,其基础位以格雷码顺序存储。这里是:

template<typename UnsignedInt>
struct gray_code
{
    static_assert(std::is_unsigned<UnsignedInt>::value,
                  "gray code only supports built-in unsigned integers");

    // Variable containing the gray code
    UnsignedInt value;

    // Default constructor
    constexpr gray_code()
        = default;

    // Construction from UnsignedInt
    constexpr explicit gray_code(UnsignedInt value):
        value( (value >> 1) ^ value )
    {}

    // Other methods...
};

在一些通用算法中,我写了这样的东西:

template<typename UnsignedInt>
void foo( /* ... */ )
{
    gray_code<UnsignedInt> bar{};
    // Other stuff...
}

在这段代码中,我希望 bar 初始化为零,因此 bar.value 初始化为零。然而,在与意想不到的错误作斗争之后,似乎 bar.value 被初始化为垃圾(准确地说是 4606858)而不是 0u。这让我很吃惊,所以我去 cppreference.com 看看上面那行到底应该做什么......


据我所知,T object{}; 形式对应于 value initialization .我觉得这句话很有趣:

In all cases, if the empty pair of braces {} is used and T is an aggregate type, aggregate-initialization is performed instead of value-initialization.

但是,gray_code 有一个用户提供的构造函数。因此它不是一个集合,因此 aggregate initialization不执行。 gray_code 没有构造函数采用 std::initializer_list 所以 list initialization也不执行。 gray_code 的值初始化应该遵循通常的 C++14 值初始化规则:

1) If T is a class type with no default constructor or with a user-provided default constructor or with a deleted default constructor, the object is default-initialized.

2) If T is a class type without a user-provided or deleted default constructor (that is, it may be a class with a defaulted default constructor or with an implicitly-defined one) then the object is zero-initialized and then it is default-initialized if it has a non-trivial default constructor.

3) If T is an array type, each element of the array is value-initialized.

4) Otherwise, the object is zero-initialized.

如果我没看错,gray_code 有一个显式默认的(不是用户提供的)默认构造函数,因此 1) 不适用。它有一个默认的默认构造函数,所以 2) 适用:gray_codezero-initialized .默认的默认构造函数似乎满足了普通默认构造函数的所有要求,因此不应发生默认初始化。那么我们来看看gray_code是如何被零初始化的:

  • If T is a scalar type, the object's initial value is the integral constant zero implicitly converted to T.

  • If T is an non-union class type, all base classes and non-static data members are zero-initialized, and all padding is initialized to zero bits. The constructors, if any, are ignored.

  • If T is a union type, the first non-static named data member is zero-initialized and all padding is initialized to zero bits.

  • If T is array type, each element is zero-initialized

  • If T is reference type, nothing is done.

gray_code 是非 union 类类型。因此,它的所有非静态数据成员都应该被初始化,这意味着 value 是零初始化的。 value满足std::is_unsigned,因此是一个标量类型,这意味着它应该用“整数常量零隐式转换为T”来​​初始化。

所以,如果我没看错的话,在上面的函数 foo 中,bar.value 应该总是用 0 初始化并且它永远不应该用垃圾初始化,对吗?

注意:我编译代码时使用的编译器是 MinGW_w4 GCC 4.9.1(POSIX 线程和 dwarf 异常),以防万一。虽然我有时会在我的计算机上得到垃圾,但我从来没有设法通过在线编译器得到零。


更新:似乎是一个 GCC 错误,错误是我的,而不是我的编译器的错误。实际上,在写这个问题时,为了简单起见,我假设

class foo {
    foo() = default;
};

class foo {
    foo();
};

foo::foo() = default;

是等价的。他们不是。这是来自 C++14 标准的引用,第 [dcl.fct.def.default] 部分:

A function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration.

换句话说,当我得到垃圾值时,我的默认默认构造函数确实是用户提供的,因为它没有在第一次声明时显式错误。因此,发生的不是零初始化而是默认初始化。再次感谢@Columbo 指出真正的问题。

最佳答案

So, if I read correctly all of that, in the function foo above, bar.value should always be initialized with 0 and it should never be initialized with garbage, am I right?

是的。您的对象是直接列表初始化的。 C++14 的* [dcl.init.list]/3 指定

List-initialization of an object or reference of type T is defined as follows:

  • [… Inapplicable bullet points…]

  • Otherwise, if T is an aggregate, aggregate initialization is performed (8.5.1).

  • Otherwise, if the initializer list has no elements and T is a class type with a default constructor, the object is value-initialized.

  • […]

您的类不是聚合,因为它具有用户提供的构造函数,但它确实具有默认构造函数。 [dcl.init]/7:

To value-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9) with either no default constructor (12.1) or a default constructor that is user-provided or deleted, then the object is default-initialized;

  • if T is a (possibly cv-qualified) class type without a user-provided or deleted default constructor, then the object is zero-initialized and the semantic constraints for default-initialization are checked, and if T has a non-trivial default constructor, the object is default-initialized;

[dcl.fct.def.default]/4:

A special member function is user-provided if it is user-declared and not explicitly defaulted […] on its first declaration.

所以你的构造函数不是用户提供的,因此对象是零初始化的。 (构造函数没有被调用,因为它微不足道)

最后,如果不清楚,零初始化 T 类型的对象或引用意味着:

  • if T is a scalar type (3.9), the object is initialized to the value obtained by converting the integer literal 0 (zero) to T;

  • if T is a (possibly cv-qualified) non-union class type, each non-static data member and each base-class subobject is zero-initialized and padding is initialized to zero bits;

  • […]


因此要么

  • 你的编译器有问题

  • …或者您的代码在其他某个时间点触发了未定义的行为。


* 在 C++11 中答案仍然是肯定的,尽管引用的部分不等价。

关于c++ - 值初始化 : default initialization or zero initialization?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26699720/

相关文章:

C++ 结构初始化

c++ - vs code c++ 断点不能在 mac 中工作

asp.net-mvc - 为什么MVC提供的Default AccountController中有2个构造函数?

struct - 在 Rust 中设置对象参数的性能

C++:初始化变量;重载隐式构造函数?

c++ - 我可以在头文件中初始化一个本身是类的数据成员吗?

c++ - (C++) 随机数相等,但程序说它们不相等

c++ - 使用默认构造函数指向对象的指针数组

C++:从结构中的静态函数中提取参数类型

java - 为什么 Java 找不到我的构造函数?