The capability to inline a function call is both a compiler-specific optimization and a common behavior. That is, many compilers can do it, but they aren't required to.
Why not
sizeof "[]" - 1;
(minus one for the trailing null. You could do sizeof "[]" - sizeof '\0', but sizeof '\0' is often sizeof( int ) in C, and "- 1 " is perfectly readable.)
#define TWO 2
#define STRING_LENGTH 2
/* ... etc ... */
Seriously, why go through all this hassle just to avoid typing a 2? I honestly think you're making your code less readable, and other programmers are going to stare at you like you're snorting the used coffee from the filter.
I think most compilers will optimize it away when optimizations are enabled. If they're disabled, it might slow your program down much more than necessary.
I would prefer your template functions, as they're guaranteed to not call strlen
at runtime.
Of course, rather than writing separate functions for char
and wchar_t
, you could add another template argument, and get a function which works for any type:
template <typename Char_t, int len>
int static_strlen(const Char_t (&)[N] array){
return len / sizeof(Char_t) - 1;
}
(As already mentioned in comments, this will give funny results if passed an array of ints, but are you likely to do that? It's meant for strings, after all)
A final note, the name _strlen
is bad. All name at namespace scope beginning with an underscore are reserved to the implementation. You risk some nasty naming conflicts.
By the way, why is "[]" less of a magic constant than 2 is?
In both cases, it is a literal that has to be changed if the format of the string it is compared to changes.