T v{} initialization

前端 未结 2 553
深忆病人
深忆病人 2021-02-12 14:40

I\'m reading the C++11 standard, but can\'t figure out whether

T x{};

is value-initialized or default initialized (automatic storage). It

2条回答
  •  星月不相逢
    2021-02-12 15:35

    Although Matt McNabb has already covered this, I will add that if you have trouble navigating through the standard, it doesn't hurt to check out cppreference. Their section on list initialization breaks it down pretty well.

    Essentially, like your standard quote says, T x{}; refers to an:

    initialization of a named variable with a brace-enclosed list of expressions or nested lists (braced-init-list).

    And:

    The effects of list initialization of an object of type T are:

    • If the braced-init-list is empty and T is a class type with a default constructor, value-initialization is performed.

    [...]

    • Otherwise, if the braced-init-list has no elements, T is value-initialized.

提交回复
热议问题