Default values in C++ initializer lists

前端 未结 3 636
失恋的感觉
失恋的感觉 2020-12-14 03:27

I only just learned yesterday that specifying parameters to initializer list items is optional. However, what are the rules for what happens in this case?

In the bel

3条回答
  •  一整个雨季
    2020-12-14 03:58

    In the below example, will ptr be initialized to 0, toggle to false, and Bar default-constructed?

    Yes. If a member initialiser appears in the initialiser list with empty parentheses, then that member is value initialised. This means that numerical types will be initialised to zero, pointers to null, and classes with default constructors using that constructor.

    If you don't include the member in the initialiser list at all, then it will instead be default initialised; in that case. numerical and pointer types will be left uninitialised.

    Could I also be pointed to the section of the C++ standard that states the behavior in the case of initializer list items not being given arguments?

    C++11 12.6.2/7 specifies that the rules are the same as for direct initialisation.

    C++11 8.5/16 specifies that if the initialiser is (), the object is value-initialised.

    C++11 8.5/7 defines value initialisation.

提交回复
热议问题