Here's a C++11 version that might be useful to you (largely untested,
and I'm only on my first cup of coffee, so... ;>).
-Jesse
// C++11 constexpr string length:
#include <iostream>
#include <cstring>
size_t constexpr string_length(const char *s)
{
return 0 == *s ? 0 : 1 + string_length(1 + s);
}
int main()
{
{
constexpr const char *s = "Howdy!";
constexpr auto len = string_length(s);
auto runtime_len = strlen(s);
std::cout << len << ", " << runtime_len << '\n';
}
{
constexpr const char *s = "";
constexpr auto len = string_length(s);
auto runtime_len = strlen(s);
std::cout << len << ", " << runtime_len << '\n';
}
// ...note that trying to pass in a nullptr is not allowed in
// a constexpr.
}