improve the wordings and readability and reduce redundant words while keeping the exact…
- User
improve the wordings and readability and reduce redundant words while keeping the exact meaning: """
Direct uniform initialization for specific classes with returned initializer list (C++11)
{}literal createsinitializer_list<T>with same-type items:cppauto foo = {1, 2}; // std::initializer_list<int>When a function uses
return {initializer_list};syntax, the compiler uses uniform initialization to directly initialize the return value3 Class Types Supporting Uniform Initialization
- Classes with Initializer List Constructors
- Classes with an
initializer_listconstructor support List Initialization - You can return actual
initializer_listinstance - Using
return {initializer_list};must contain elements of same type
cppauto foo (int val) -> std::vector<int>{ if (val > 42) { auto il = {1, 2}; return il; } // equivalent to std::vector<int>{1,2} // Invokes std::vector's std::initializer_list<T>&& constructor return {1, 2}; };- Aggregate types
An aggregate type is a class or struct that:
- has no user-declared or inherited constructors
- has no base classes
- has no virtual functions
- all of its non-static data members are public
- intended to be simple data carrier that contains fundamental types or aggregates
Uniform initialization on aggregate types via
{}:-
performs Aggregate Initialization
-
values are directly assigned to members
-
You cannot return an
initializer_listinstance as aggregate types do not have constructors -
Using
return {initializer_list};can contain elements of different types
cpp - Assistant