Short version: Is __builtin_strlen constexpr? or Where could I find a specification of builtin functions?
I'm using gcc with libc++ from LLVM and have got a problem with following code:
#include <string_view>
constexpr bool test() {
const char s1[] = "mave";
std::string_view s2(s1);
return true;
}
int main() {
static_assert(test());
}
Error message was:
$ g++ -nostdinc++ -I/home/yiyan/.local/include/c++/v1 -nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc_s -lgcc -std=c++17 --compile -o /dev/null 1.cc
1.cc: In function 'int main()':
1.cc:8:23: error: non-constant condition for static assertion
8 | static_assert(test());
| ~~~~^~
In file included from /home/yiyan/.local/include/c++/v1/string_view:175,
from 1.cc:1:
1.cc:8:23: in 'constexpr' expansion of 'test()'
1.cc:4:27: in 'constexpr' expansion of 's2.std::__1::basic_string_view<char>::basic_string_view(((const char*)(& s1)))'
/home/yiyan/.local/include/c++/v1/string_view:238:46: in 'constexpr' expansion of 'std::__1::char_traits<char>::length(__s)'
/home/yiyan/.local/include/c++/v1/__string:217:69: error: '__builtin_strlen(((const char*)(& s1)))' is not a constant expression
217 | length(const char_type* __s) _NOEXCEPT {return __builtin_strlen(__s);}
| ~~~~~~~~~~~~~~~~^~~~~
It seems to be caused by __builtin_strlen being not constexpr, and I tried to get a repro as this:
const char s[] = "";
constexpr int test(const char *s) {
return __builtin_strlen(s);
}
int main() {
static_assert(!test(s));
return 0;
}
In this case gcc compiled successfully and looks like __builtin_strlen is constexpr, so I'm a little bit confused.
So I wonder where could I find a specification of builtin functions and how should I address this issue.
Best,
Yichen