I have the following code:
static const char* ITEMS[] = { "a", "b" } // too many elements for a std::array in my situation
void my_func() {
printf("a: %d", index_of(ITEMS, "a")); // 0
printf("d: %d", index_of(ITEMS, "d")); // error at compile time; "d" does not exist in ITEMS.
}
How would I define a similar index_of method? I've seen various suggestions on other SO posts, but they don't work for the following reasons:
- Use
std::string/MakeITEMSaconstexpr- Unfortunately ImGui's
Comborequires aconst char**for the array type which is impossible withstd::string, and similarly, asITEMSwould be aconstexprit would be of typeconst char* const *, which does not work.
- Unfortunately ImGui's
- Use
std::arrayinstead ofT[]- The number of elements is too high for the compiler to handle, resulting in a compiler error.
std::array<T,N>is just a wrapper for aT[N]array, so they should have the same compiler limitations. "The number of elements is too high for the compiler to handle, resulting in a compiler error" - that would imply that your design is wrong to begin with and should be rewritten.const. If the pointers may be modified by a function, then they are not compile-time constants.arraynot being inferred, but other than that it's as Remy says: Same size.In template: instantiating fold expression with 1821 arguments exceeded expression nesting limit of 256> It can only possibly work at compile-time if it is guaranteed that the pointers themselves are const How would I specify this in code? None of them are modified at runtime.