Question: How can I ensure COLOR\_NAMES is filled to the correct size at compile-time? If a new color is added, say COLOR\_4 (and hence N\_COLORS is auto incremented), the compiler will then tell me COLOR\_NAMES is not filled to size.

Most answers that I found online is for runtime, not compile time.

This is for C-style notation (no STL and other libraries usage).

```
enum Colors
{
   COLOR_1,
   COLOR_2,
   COLOR_3,
   N_COLORS;
};

const char* COLOR_NAMES[N_COLORS] =
{
   /* COLOR_1 */ "Color1",
   /* COLOR_2 */ "Color2",
   /* COLOR_3 */ "Color3"
};

const char* Blah()
{
   Colors color;
   ...
   printf("%s blah blah\n", COLOR_NAMES(color));
}

```

Answer: 
It is standard practice for such cases of arrays and corresponding enums to compare the "enum size member", `N_COLORS` in your case, against the number of items in the array.

To get the number of items in an array, simply divide the array size with the size of one array member.

Thus:

```
_Static_assert(sizeof(COLOR_NAMES)/sizeof(*COLOR_NAMES) == N_COLORS, 
               "Array item missing!");

```

**Edit:**

Oh btw for this to be meaningful the array declaration must be `const char* COLOR_NAMES[] =` otherwise you wouldn't be able to tell if there are missing initializers in the array initialization list.
with constepxr, you can use :

```
constexpr static const char* COLOR_NAMES[N_COLORS] =
{
   /* COLOR_1 */ "Color1",
   /* COLOR_2 */ "Color2",
   /* COLOR_3 */ "Color3"
};

static_assert(COLOR_NAMES[N_COLORS-1] !=0);

```

if you want to use local variable, you can use variant

```
constexpr variant<const char*> COLOR_NAMES[N_COLORS] =
{
   /* COLOR_1 */ "Color1",
   /* COLOR_2 */ "Color2",
   /* COLOR_3 */ "Color3"
};

static_assert(get<0>(COLOR_NAMES[N_COLORS-1]) !=nullptr);

```