Reputation: 7818
I have some C that I need to convert to C++.
It does something like this:
enum
{
ELEM0,
ELEM1,
ELEM2,
ELEM3,
ELEM4,
MAX_ELEMS
}
#define LEN 16
static const char lut[MAX_ELEMS][LEN] =
{
[ELEM2] = "Two",
[ELEM3] = "Three",
[ELEM1] = "One",
[ELEM4] = "Four",
[ELEM0] = "Zero"
}
In practice I have hundreds of elements without any order in the array. I need to guarantee that the entry in the array ties up the enumeration to the appropriate text.
Is it possible to initialise an array using positional parameters like this in -std=gnu++11?
Upvotes: 6
Views: 211
Reputation: 56549
No. Per gcc documentation, Designated Initializers do not exists in GNU C++.
In ISO C99 you can give the elements in any order, specifying the array indices or structure field names they apply to, and GNU C allows this as an extension in C89 mode as well. This extension is not implemented in GNU C++.
Doesn't this solve you problem (although it's run-time):
static const std::map<int, std::string> lut =
{
std::make_pair(ELEM2, "Two"),
std::make_pair(ELEM3, "Three"),
std::make_pair(ELEM1, "One"),
};
Upvotes: 3