[libcxx-dev] Problem with __builtin_strlen among compilers

Marshall Clow via libcxx-dev libcxx-dev at lists.llvm.org
Tue Sep 10 10:19:52 PDT 2019


On Mon, Sep 9, 2019 at 1:50 PM Yan Yichen via libcxx-dev <
libcxx-dev at lists.llvm.org> wrote:

> I got a little bit confused trying to get a repro of __builtin_strlen in
> libc++ with gcc.
>
> At least the behavior of __builtin_strlen is different in clang and gcc.
>
>
>
> This passed
>
>
>
> g++ -o /dev/null -std=c++17 --compile test2.cc
>
> while this failed
>
>
>
> clang++ -o /dev/null -std=c++17 --compile test2.cc
>
> with error messages:
>
>
>
> test2.cc:6:19: error: static_assert expression is not an integral constant
> expression
>
>     static_assert(!test(s));
>
>                   ^~~~~~~~
>
> test2.cc:3:12: note: read of non-constexpr variable 's' is not allowed in
> a constant expression
>
>     return __builtin_strlen(s);
>
>            ^
>
> test2.cc:6:20: note: in call to 'test(&s[0])'
>
>     static_assert(!test(s));
>
>                    ^
>
> 1 error generated.
>
>
>
> For one same file:
>
>
>
>
>
> const char s[] = "";
>
> constexpr int test(const char* s) {
>
>     return __builtin_strlen(s);
>
> }
>
> constexpr void __main() {
>
>     static_assert(!test(s));
>
> }
>
>
>
>
Well, sure.
The global variable s is not constexpr in your example.

-- Marshall
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/libcxx-dev/attachments/20190910/70797ada/attachment.html>


More information about the libcxx-dev mailing list