[llvm-dev] RFC: Constructing StringRefs at compile time
Mehdi Amini via llvm-dev
llvm-dev at lists.llvm.org
Fri Nov 25 10:50:56 PST 2016
> On Nov 25, 2016, at 4:34 AM, Malcolm Parsons via llvm-dev <llvm-dev at lists.llvm.org> wrote:
>
> On 24 November 2016 at 15:04, Hal Finkel <hfinkel at anl.gov> wrote:
>>> Creating constexpr StringRefs isn't trivial as strlen isn't portably
>>> constexpr and std::char_traits<char>::length is only constexpr in
>>> C++17.
>>
>> Why don't we just create our own traits class that has a constexpr length, and then we can switch over to the standard one when we switch to C++17?
>
> GCC and Clang treat __builtin_strlen as constexpr.
> MSVC 2015 doesn't support C++14 extended constexpr. I don't know how
> well it optimises a recursive strlen.
>
> This works as an optimisation for GCC and Clang, and doesn't make
> things worse for MSVC:
>
> /// Construct a string ref from a cstring.
> LLVM_ATTRIBUTE_ALWAYS_INLINE
> +#if __has_builtin(__builtin_strlen)
> + /*implicit*/ constexpr StringRef(const char *Str)
> + : Data(Str), Length(Str ? __builtin_strlen(Str) : 0) {}
> +#else
> /*implicit*/ StringRef(const char *Str)
> : Data(Str), Length(Str ? ::strlen(Str) : 0) {}
> +#endif
The only reason I didn’t go with this solution was that an MSVC built clang would take a long time to startup if StringRef are present in global tables.
—
Mehdi
More information about the llvm-dev
mailing list