Initialization of certain elements of a const const array in C ++

I have a C that I need to convert to C ++.

He 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 ensure that the entry in the array associates the enumeration with the corresponding text.

Is it possible to initialize an array using positional parameters like in -std = gnu ++ 11?

+6
source share
1 answer

No. Per gcc documentation , designated initializers do not exist in GNU C ++.

In ISO C99, you can specify elements in any order by specifying the array indexes or the names of the structure fields to which they apply, and GNU C allows this as an extension in C89 mode. This extension is not implemented in GNU C ++.

Does your problem solve (although this is runtime):

 static const std::map<int, std::string> lut = { std::make_pair(ELEM2, "Two"), std::make_pair(ELEM3, "Three"), std::make_pair(ELEM1, "One"), }; 
+3
source

Source: https://habr.com/ru/post/956952/


All Articles