简体   繁体   中英

Is this a list initialization or a value initialization?

int i {};

Is this List Initialization or Value Initialization ?

I can't distinguish them because I can't understand this sentence: a possibly empty brace-enclosed list of expressions or nested braced-init-lists from the link: https://en.cppreference.com/w/cpp/language/list_initialization

Is this List Initialization or Value Initialization?

It is direct-list-initialization

T object { arg1, arg2, ... }; (1)

direct-list-initialization (both explicit and non-explicit constructors are considered)

  1. initialization of a named variable with a braced-init-list (that is, a possibly empty brace-enclosed list of expressions or nested braced-init-lists)

and the effect on T isvalue-initialization

Explanation

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

[...]

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

And now for the value-initialization of int eger you get zero-initialized .

Explanation

Value initialization is performed in these situations:

[...]

  1. otherwise, the object is zero-initialized.

Whether it is a List or Value initialization depends on the object you are initializing. Seehttps://en.cppreference.com/w/cpp/language/value_initialization :

If T is a class type that has no default constructor but has a constructor taking std::initializer_list, list-initialization is performed.

So since the object in this case in int which does not have a constructor taking a std::initializer_list and int is not an aggregate type, this isvalue initialization .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM