views:

464

answers:

4
+2  A: 

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.

Rob Kennedy
The desired optimisation doesn't (just) require inlining. It requires that the length of the string be computed at compile time.
Steve Jessop
That's not really an optimization, though. The length wouldn't be computed at run time and still call any `_lenof` function. Doesn't the standard *require* implementations to give string literals the type `const char[N]`? And aren't values of such type required to make the compiler deduce the arguments of the template function to be `N`?
Rob Kennedy
Sorry, I misunderstood what your answer was referring to - for some reason I though you were talking about "not efficient because of [the strlen] function call". If a compiler can't inline _lenof, then it probably can't inline anything, and would be a pretty poor C++ compiler in general. Any serious use of templates would be nightmarish...
Steve Jessop
+3  A: 

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.)

William Pursell
is wouldn't work for wide strings (for example, L"[]").
Dmitriy
could be fixed for wide strings. Something like: `(sizeof(L"[]") / sizeof(L"")) - 1`
Evan Teran
@Evan Teran: yes, but you should use macros to make it more readable. IMHO macros it's more C style but not C++
Dmitriy
I agree, was just pointing out that this technique was usable for wide strings. I was not endorsing it :-P.
Evan Teran
A: 
#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.

Jed Smith
it's just example. In real code it looks like "some string". Are you going to count number of characters in this case? :)
Dmitriy
Yes, I am. And I will. And I do.
Jed Smith
@Jed Smith: :) Are you sure that you don't forget to change the macro definition if the string changes?
Dmitriy
@Jed Smith: How is "2" more readable/understandable then sizeof("[]") ?
Malkocoglu
@Jed: Hey, if it's only a 2, then why bother? When it's "some string I might change and I don't want to bother eyeballing the length of" it's a different story. OTOH, strlen() is only a problem when it's really a problem. Assuming it's a problem is a form of guessing.
Mike Dunlavey
+1  A: 

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.

jalf