According to the C++ reference, aggregate initialization has the following syntax:
T object = { arg1, arg2, ... };
T object { arg1, arg2, ... };
T object = { .des1 = arg1 , .des2 { arg2 } ... };
T object {.des1 = arg1 , .des2 { arg2 } ... };
So this would be an example of aggregate initialization:
int a, b;
std::size_t arr[]{a,b};
But if an aggregate is a member of a class initialized in the member initializer list, is it still an aggregate initialization or a different type of initialization?
struct S
{
template <typename T>
S(T a, T b)
: arr{ a, b }
{ }
std::size_t arr[2];
};
Update:
I thought the question is clear, but having read the comments, I realize that I have to clarify that it is about the array member arr, not about S.
>Solution :
Aggregate initialization refers to a certain semantic of initialization, not to a syntactical form of initialization.
The syntax listed by cppreference just demonstrates the possible syntactical forms that may result in aggregate initialization, but they aren’t generally aggregate initialization.
The syntactical forms are called list initialization instead. List initialization may only result in aggregate initialization if the object type is an aggregate type. But for aggregate types, with few exceptions, list initialization does result in aggregate initialization.
In the this case S::arr is an array type and so an aggregate type. Consequently arr{ a, b } in the constructor’s initializer list is aggregate initialization of arr as well.
The form of initialization is determined only be the initializer part of the syntax, i.e. { a, b }. The context in which the initializer appears, such as a variable declaration or constructor initializer list, doesn’t matter.