Tested a simple utf8 strlen function and was quite surprised that trunk clang completely eliminated it (gcc doesn't):
static int strlenutf8(const char* s)
{
int i = 0, l = 0;
while (s[i])
{
if ((s[i] & 0xc0) != 0x80) l++;
l++;
}
return j;
}
int main()
{
return strlenutf8("bla");
}
clang++ -O3 -S -fverbose-asm:
main: # @main
.cfi_startproc
# BB#0: # %entry
movl $3, %eax
ret
That's like D's compile time function evaluation. Is this even legal in C++?
I mean in the end there must be a reason why they invented that crappy constexpr in the first place. Which couldn't even be used here to my knowledge since it's heavily restricted.
constexpr
is required only for constant expression contexts (like template argument deduction), but aconstexpr
function is not guaranteed to be evaluated at compile-time.The golden rule of optimizing C++ programs is the
as-if
rule:With the much needed footnote:
With a major BUT: copy constructors with side effects (e.g. they increment a "copy constructor called" count variable or equivalent) do not need to be included in the "as-if". This is included in
12.8/31
: