[libcxx-dev] Problem with __builtin_strlen among compilers

Louis Dionne via libcxx-dev libcxx-dev at lists.llvm.org
Mon Sep 9 10:16:23 PDT 2019


I think the most productive way of resolving this issue is to file a bug against GCC asking them to make __builtin_strlen constexpr.

Alternatively, libc++ could also use `if (std::is_constant_evaluated())` to pick between __builtin_strlen and a naive version, but that won't fix your problem when compiling in C++17 mode because is_constant_evaluated() is (will be) C++20.

Louis

> On Sep 9, 2019, at 09:41, Yan Yichen via libcxx-dev <libcxx-dev at lists.llvm.org> wrote:
> 
> Hi, I’m using libc++ with gcc and have met a problem with code below:
>  
>  
> #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 <http://1.cc/>
> 1.cc <http://1.cc/>: In function ‘int main()’:
> 1.cc <http://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 <http://1.cc/>:1:
> 1.cc <http://1.cc/>:8:23:   in ‘constexpr’ expansion of ‘test()’
> 1.cc <http://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);}
>       |                                                     ~~~~~~~~~~~~~~~~^~~~~
>  
>  
> Looks like the root cause is that __builtin_strlen is not constexpr in gcc.
> Do you consider to fix this kind of issue?
>  
>  
> Best,
> Yichen
> _______________________________________________
> libcxx-dev mailing list
> libcxx-dev at lists.llvm.org <mailto:libcxx-dev at lists.llvm.org>
> https://lists.llvm.org/cgi-bin/mailman/listinfo/libcxx-dev <https://lists.llvm.org/cgi-bin/mailman/listinfo/libcxx-dev>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/libcxx-dev/attachments/20190909/1e63fa7e/attachment-0001.html>


More information about the libcxx-dev mailing list