Can I initialize an array using the std::initializer_list instead of brace-enclosed initializer?

后端 未结 3 920
轻奢々
轻奢々 2020-12-16 17:24

Can I initialize an array using the std::initializer_list object instead of brace-enclosed initializer?

As known, we can do this: http://en.cppreference

3条回答
  •  孤城傲影
    2020-12-16 18:06

    The problem with std::array is that it is required to be an aggregate type, hence it does not have constructors.

    Hence only aggregate initialization or trivial copy are possible. std::initializer_list is a class other than std::array, so a (missing) implicit conversion is required.

    See http://en.cppreference.com/w/cpp/language/aggregate_initialization and http://en.cppreference.com/w/cpp/container/array for reference.

提交回复
热议问题