[llvm-dev] RFC: General purpose type-safe formatting library

Zachary Turner via llvm-dev llvm-dev at lists.llvm.org
Wed Oct 12 13:07:11 PDT 2016


Couldn't you define a class FormatString like this:

class FormatString {
  template<int N>
  constexpr FormatString(const char (&S)[N]) {
    tokenize();
  }

  FormatString(const char *s) {}
};

Then define the format function as format(const FormatString &S, Ts
&&...Args)

The implicit conversion from string literal would go to the constexpr
constructor which could tokenize the string at compile time, while implicit
conversion from non-literal would be tokenized at runtime.

If that doesn't work, then like you said, you could use a UDL to force the
checking at compile time.

On Wed, Oct 12, 2016 at 1:01 PM Tim Shen <timshen at google.com> wrote:

> On Wed, Oct 12, 2016 at 12:35 PM Zachary Turner via llvm-dev <
> llvm-dev at lists.llvm.org> wrote:
>
> You get compile time checking automatically when we can use c++14 though.
> If you use it with a string literal, you'll get compile time checking,
> otherwise you won't.
>
>
> Even with C++14, os.format("literal format", a, b, c) cannot do the
> compile-time checking (I maybe wrong with understanding C++14 constexpr).
> You probably need to add a overloaded version like
> `os.format(static_format("literal format"), a, b, c)`, or
> `os.format("literal format"_fmt, a, b, c)` to hold the compile-time checked
> version.
>
> But anyway, the current interface os.format(const char*, ...) is
> forward-compatible.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161012/b06be627/attachment.html>


More information about the llvm-dev mailing list