Computing length of a C string at compile time. Is this really a constexpr?
问题 I\'m trying to compute the length of a string literal at compile time. To do so I\'m using following code: #include <cstdio> int constexpr length(const char* str) { return *str ? 1 + length(str + 1) : 0; } int main() { printf(\"%d %d\", length(\"abcd\"), length(\"abcdefgh\")); } Everything works as expected, the program prints 4 and 8. The assembly code generated by clang shows that the results are computed at compile time: 0x100000f5e: leaq 0x35(%rip), %rdi ; \"%d %d\" 0x100000f65: movl $0x4