Can a member struct be zero-init from the constructor initializer list without calling memset?

前端 未结 3 747
时光说笑
时光说笑 2020-12-29 05:53

Let\'s say I have the following structure declaration (simple struct with no constructor).

struct Foo
{
    int x;
    int y;
    int z;
    char szData[DATA         


        
3条回答
  •  梦谈多话
    2020-12-29 06:14

    Yes, this is defined behaviour according to the standard. 12.6.2 [class.base.init] / 3 : "if the expression-list of the mem-initializer is omitted, the base class or member subobject is value-initialized."

    Be warned, though, if Foo wasn't a POD-type but still had no user-declared constructor (e.g. it had a std::string type) then some very popular compilers would not correctly value-initialize it.

    All compilers that I know of do correctly perform value-initialization of POD members when you use () as the initializer in a constructor initializer-list.

提交回复
热议问题