Where can we use list initialization?

后端 未结 3 539
忘了有多久
忘了有多久 2020-12-08 23:12

This question already covers what PODs and aggregates are, and provides some examples on aggregate initialization.

The question here is where can you use li

3条回答
  •  [愿得一人]
    2020-12-08 23:23

    Aggregate initialization is the subset of list initialization that is limited just to aggregates and PODs (as discussed in the question you referenced). Both types of initialization make use of curly braces and optionally and an equals, so the syntax does appear the same at the point of initialization. See http://en.cppreference.com/w/cpp/language/aggregate_initialization and http://en.cppreference.com/w/cpp/language/list_initialization for more details including places where each form of initialization can be used.

    In C++03 aggregate initialization could only be used with equals (i.e. T object {arg1, arg2}; wasn't valid just T object = {arg1, arg2};), while C++11 allows it without equals (i.e. T object {arg1, arg2}; became valid). Also in C++11 aggregate initialization was modified slightly to disallow narrowing conversions in aggregate initialization.

    The subset of list initialization, which is not the aggregate initialization subset, was introduced in C++11.

提交回复
热议问题