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 level of seemingly redundant curly braces. In other words, I expect the initialization to look like:
MapType g_mapping = {
{"POST_MASTER", MessageDeliveryMethod::POST_MASTER},
{"BUBBLE", MessageDeliveryMethod::BUBBLE},
};
(One level of outer braces removed).
My understanding is that prior to C++14, when doing direct initialization the extra level of braces is required. However, copy initialization was not supposed to require this based on this page (look at the example there).
Can anyone explain this?
UPDATE:
This SO question which is presumed to be duplicated by my question does indeed answer some specific and helpful questions (related to my own) however out of context mine was confusing due to the usage of pair
(which I thought was causing the issue initially). I never would have found that SO question in the first place, so if anything I think perhaps the way I've worded my question may help people arrive to the solution from different angles.