Initializing an std :: array aggregate requires a confusing number of curly braces

I have the following code:

enum class MessageDeliveryMethod { POST_MASTER, BUBBLE, NUM_ENUMERATORS }; namespace { using MapType = std::array< std::pair<char const*, MessageDeliveryMethod>, static_cast<std::size_t>(MessageDeliveryMethod::NUM_ENUMERATORS) >; MapType g_mapping = {{ {"POST_MASTER", MessageDeliveryMethod::POST_MASTER}, {"BUBBLE", MessageDeliveryMethod::BUBBLE}, }}; } 

This compiles, but I donโ€™t know why. The g_mapping variable requires an extra layer of seemingly redundant curly braces. In other words, I expect initialization to look like this:

 MapType g_mapping = { {"POST_MASTER", MessageDeliveryMethod::POST_MASTER}, {"BUBBLE", MessageDeliveryMethod::BUBBLE}, }; 

(One level of outer braces removed).

I understand that prior to C ++ 14, direct initialization requires an extra level of curly braces. However, copy initialization should not require this based on this page (look at the example there).

Can anyone explain this?

UPDATE:

This SO question , which is supposed to be duplicated by my question, really answers some specific and useful questions (related to my own), however, out of context, mine got confused due to the use of pair (which, as I thought, was initially causing the problem). I would never have found this question in the first place, therefore, if something that I think is possible, as I formulated my question, can help people come to a solution from different angles.

+7
c ++ c ++ 11
source share
3 answers

std::array is defined as a structure containing an array.

Thus, the first pair of braces is used to initialize the data elements of the structure, which is an array. The second pair of braces is used to initialize the array inside the structure. And the third pairs of braces are used to initialize each object of type std :: pair.

More precisely, then according to the C ++ standard (an overview of the array of class templates 23.3.2.1)

2 An array is an aggregate (8.5.1) that can be initialized using Syntax

 array<T, N> a = { initializer-list }; 

where the initializer list is a comma-separated list of up to N elements whose types are converted to T.

+5
source share

std::array defined as a nested aggregate - a class containing an array as its only member. Prior to C ++ 14, aggregate initialization required two levels of curly braces: one to surround the list of class members (in which there was only one element, an array) and one to surround the list of array elements. Then you need the third level, if, as here, you want to list-initialize each element of the array.

C ++ 14 allows you to โ€œsmooth outโ€ lists when initializing nested aggregates, as described on the page you are linking to.

+1
source share

In the standard, we have 23.3.2.1 (3)

An array is an aggregate (8.5.1) that can be initialized with syntax

array<T, N> a = { initializer-list };

This shows that when you initialize an array using a list of internalizers, you need to wrap it in braces.

0
source share

All Articles