0

Consider we have an array of strings:

static constexpr char *sCommands[] = {
    "AAA",
    "CCC",
    "BBB",
    "DDD",
    ...
};

Can we get it sorted at compile time (or get another sorted array) without using std? Template is allowed.

Edit

Some background: This list will be binary-searched so it has to be sorted. The intention for doing the compile-time sorting is: There will be a lot of macros wrapping these items in this list and it's hard to maintain.

For example:

static constexpr char *sCommands[] = {
#if FEATURE_1
    "AAA",
#endif
#if FEATURE_2
    "BBB",
#endif
#if FEATURE_1
    "CCC",
#endif
#if FEATURE_2
    "DDD",
#endif
    ...
};

In real cases, this is more complex with nested macros, which makes the code hard to read and maintain.

I would be better if we can put the items under the macros:

static constexpr char *sCommands[] = {
#if FEATURE_1
    "AAA",
    "CCC",
#endif
#if FEATURE_2
    "BBB",
    "DDD",
#endif
    ...
};
oos1111
  • 23
  • 6

0 Answers0