The following compiles in GCC but not in Clang:
#include <cstring>
constexpr int test = strcmp("test", "test");
So my question is, how does GCC handle strcmp differently to make this possible? Is strcmp some type of builtin, or does its standard library have a non-standard definition of strcmp that includes constexpr?
The code compiles on gcc because it provides a built-in version of strcmp
that is evaluated at compile time, assuming you pass string literals to the function.
gcc will reject the code if you pass the -fno-builtin
(or -fno-builtin-strcmp
) flag.
Nothing forbids a function from being constexpr
. There's just no guarantee that strcmp
is constexpr
. A good compiler will probably be able to perform string operations on compile-time constant strings efficiently, but that's an implementation detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With