[llvm-dev] RFC: Constructing StringRefs at compile time

Malcolm Parsons via llvm-dev llvm-dev at lists.llvm.org
Thu Nov 24 06:59:25 PST 2016

Hi all,

There is a desire to be able to create constexpr StringRefs to avoid
static initializers for global tables of/containing StringRefs.

Creating constexpr StringRefs isn't trivial as strlen isn't portably
constexpr and std::char_traits<char>::length is only constexpr in

Alp Toker tried to create constexpr StringRefs for strings literals by
subclassing StringRef:
This is a verbose change where needed at string literal call sites.

Mehdi AMINI tried to add a constexpr constructor for string literals
by making the constructor from const char * explicit:
This is a verbose change at every non-literal call site.
This only works with assignment syntax.

I've suggested using a user-defined literal:
This is a small change where needed at string literal call sites.
C++17 adds a UDL for std::string_view, so it's not an unusual idea.
There is resistance to using a UDL as they can introduce a surprising
and novel syntax for calling functions.


Other options?

Malcolm Parsons

More information about the llvm-dev mailing list