值初始化:默认初始化或零初始化? [英] Value initialization: default initialization or zero initialization?

查看:294
本文介绍了值初始化:默认初始化或零初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有模板 gray_code 类,这意味着存储一些无符号整数,其底层位以格雷码顺序存储。这是:

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

//包含灰色代码的变量
UnsignedInt value;

//默认构造函数
constexpr gray_code()
= default;

//从UnsignedInt构造
constexpr explicit gray_code(UnsignedInt value):
value((value>> 1)^ value)
{}

//其他方法...
};

在某些通用算法中,我写了如下:

 模板< typename UnsignedInt> 
void foo(/ * ... * /)
{
gray_code< UnsignedInt>酒吧{};
//其他材料...
}

,我希望 bar 为零初始化,因此 bar.value 为零初始化。但是,在遇到意外错误后,看起来 bar.value 初始化为垃圾(4606858是准确的),而不是 0u 。这让我很惊讶,所以我去cppreference.com看看上面的行是应该做什么...






从我可以读到,形式 T object {}; 对应于值初始化。我发现这个引号很有趣:


在所有情况下,如果使用空的大括号{}并且T是聚合类型,


但是, gray_code 用户提供的构造函数。因此,它不是一个聚合,因此不执行聚合初始化 gray_code 没有构造函数采用 std :: initializer_list ,因此列表初始化。然后, gray_code 的值初始化应该遵循通常的C ++ 14价值初始化规则:


1)如果T是没有默认构造函数或用户提供的默认构造函数或删除的默认构造函数的类类型,则该对象是默认初始化的。



2)如果T是没有用户提供或删除的默认构造函数的类类型(也就是说,它可能是一个具有默认默认构造函数的类,定义一个),那么对象是零初始化,然后如果它有一个非平凡的默认构造函数,它是默认初始化的。



3)如果T是一个数组类型,数组的每个元素都是值初始化的。



否则,对象是零初始化的。


如果我正确读取, gray_code 有一个显式默认的(不是用户提供的)默认构造函数,因此1)不适用。它有一个默认的默认构造函数,所以2)适用: gray_code 零初始化。默认的默认构造函数似乎满足一个琐碎的默认构造函数的所有要求,因此默认初始化不应该发生。让我们来看看 gray_code 是否为零初始化:



  • 如果T是标量类型,则对象的初始值是隐式转换为T的积分常数零。


  • 非联合类类型,所有基类和非静态数据成员被零初始化,并且所有填充被初始化为零比特。构造函数(如果有)将被忽略。


  • 如果T是联合类型,则第一个非静态命名数据成员是零初始化,并且所有填充都初始化为零位。


  • 如果T是数组类型



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



所以,如果我正确读取所有的,在函数 foo 上面, bar.value 应始终用 0 初始化,它不应该用垃圾初始化,



注意:我编译我的代码的编译器是MinGW_w4 GCC 4.9.1 with(POSIX threads and dwarf exception)虽然我有时在我的电脑上收到垃圾,但我从来没有设法通过在线编译器得到零。






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

  class foo {
foo默认;
};

  class foo {
foo();
};

foo :: foo()= default;

是等价的。他们不是。下面是C ++ 14标准的部分[dcl.fct.def.default]的引用:


函数




如果用户已声明且未明确默认或
已删除, p>换句话说,当我得到垃圾值,我默认的默认构造函数确实是用户提供的,因为它没有明确的efaulted在其第一个声明。因此,发生了什么不是零初始化,而是默认初始化。感谢@Columbo再次指出真正的问题。

解决方案


在上面的函数 foo 中,
bar.value 应始终使用 0 ,它应该永远不会是
用垃圾初始化,我是对吗?


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


类型<$的对象或引用的列表初始化c $ c> T 定义为
,如下所示:




  • [...不适用的项目符号points ...]


  • 否则,如果 T )。


  • 否则,如果初始化器列表没有元素,T是一个带默认构造函数的类类型, >


  • [...]



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


值初始化类型 T 表示:




  • if T 是没有默认构造函数(12.1)或
    是用户提供或删除的默认构造函数
    的(可能是cv限定的)类类型(第9条)没有用户提供或删除的默认构造函数,则默认初始化


  • ,那么对象是
    零初始化并且检查
    默认初始化的语义约束,并且如果 T 具有非平凡的
    默认构造函数,对象是默认初始化的;



fct.def.default] / 4:


特殊成员函数是用户提供的


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



最后,如果这不清楚,要对对象或引用进行零初始化 T 类型表示:



  • 如果 T 是标量类型(3.9),则将对象初始化为通过转换整数文本 0 (零)到 T ;


  • c>是(可能是cv限定的)非联合类类型,每个非静态数据成员和每个基类子对象被零初始化,并且填充被初始化为零位;


  • [...]



b $ b因此


p> ...或者你的代码在其他点触发未定义的行为。







*答案在C ++ 11中仍然是,但引用的部分不等同。


I have templated gray_code class which is meant to store some unsigned integer whose underlying bits are stored in Gray code order. Here it is:

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...
};

In some generic algorithm, I wrote something like this:

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

In this piece of code, I expected bar to be zero-intialized and therefore bar.value to be zero-initialized. However, after having struggled with unexpected bugs, it appears that bar.value is initialized with garbage (4606858 to be exact) instead of 0u. That surprised me, so I went to cppreference.com to see what the line above was exactly supposed to do...


From what I can read, the form T object{}; corresponds to value initialization. I found this quote interesting:

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.

However, gray_code has a user-provided constructor. Therefore it is not an aggregate thus aggregate initialization is not performed. gray_code has no constructor taking an std::initializer_list so list initialization is not performed either. The value-initialized of gray_code should then follow the usual C++14 rules of value initialization:

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.

If I read correctly, gray_code has an explicitly defaulted (not user-provided) default constructor, therefore 1) does not apply. It has a defaulted default constructor, so 2) applies: gray_code is zero-initialized. The defaulted default constructor seems to meet all the requirements of a trivial default constructor, so default initialization should not happen. Let's have a look then at how gray_code is zero-initialized:

  • 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 is a non-union class type. Therefore, all of its non-static data members should be initialized which means that value is zero-initialized. value satisfies std::is_unsigned and is therefore a scalar type, which means that it should be initialized with "the integral constant zero implicitly converted to T".

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?

Note: the compiler I compiled my code with is MinGW_w4 GCC 4.9.1 with (POSIX threads and dwarf exceptions) in case that helps. While I sometimes get garbage on my computer, I never managed to get anything else than zero with online compilers.


Update: It seems to be a GCC bug that the error is mine and not that of my compiler. Actually, when writing this question, I assumed for the sake of simplicity that

class foo {
    foo() = default;
};

and

class foo {
    foo();
};

foo::foo() = default;

were equivalent. They are not. Here is the quote from the C++14 standard, section [dcl.fct.def.default]:

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

In other words, when I got garbage values, my defaulted default constructor was indeed user-provided since it was not explicitly efaulted on its first declaration. Therefore, what happened was not zero initialization but default initialization. Thanks @Columbo again for pointing out the real problem.

解决方案

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?

Yes. Your object is direct-list-initialized. C++14's* [dcl.init.list]/3 specifies that

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.

  • […]

Your class isn't an aggregate since it has user-provided constructors, but it does have a default constructor. [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.

So your constructor is not user-provided, therefore the object is zero-initialized. (The constructor is not called since its trivial)

And finally, in case this was not clear, to zero-initialize an object or reference of type T means:

  • 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;

  • […]


Thus either

  • Your compiler is bugged

  • …or your code triggers undefined behavior at some other point.


* The answer is still yes in C++11, though the quoted sections are not equivalent.

这篇关于值初始化:默认初始化或零初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆