I have a class where I use the length of the string literal as a template parameter. This way, the length of the literal is evaluated at compile time, and there's no reason to do strlen
(or equivalent) at runtime. I would like to do the same with an array of string literals.
// Handler class
class Handler {
template<size_t N>
bool String(const char(&str)[N]) { /* ... */ }
};
Handler myHandler;
// Works
myHandler.String("it");
// Handler::String(const char (&)[N])': could not deduce template argument for 'const char (&)[N]' from 'const char *const *'
const char* manyIts[3] = {"this", "and", "that" };
for (int i = 0; i < 3; ++i) {
myHandler.String(manyIts[i]);
}
I understand that fact the strings are literals is lost when storing them in a const char*
array. Is there some other convenient way to store these to allow this sort of functionality?