<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Wed, Oct 12, 2016 at 1:07 PM Zachary Turner <<a href="mailto:zturner@google.com">zturner@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="gmail_msg">Couldn't you define a class FormatString like this:<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">class FormatString {</div><div class="gmail_msg">  template<int N></div><div class="gmail_msg">  constexpr FormatString(const char (&S)[N]) {</div><div class="gmail_msg">    tokenize();</div><div class="gmail_msg">  }</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">  FormatString(const char *s) {}</div><div class="gmail_msg">};</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Then define the format function as format(const FormatString &S, Ts &&...Args)</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">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.</div></div></blockquote><div><br></div><div>Actually it might be doable even in C++11. For example:</div><div><br></div><div><div>  template<typename Ret></div><div>  Ret error_empty_string() {}</div><div><br></div><div>  constexpr char get_first_char(const char* a) {</div><div>    return *a == '\0' ? error_empty_string<char>() : a[0];</div><div>  }</div><div><br></div><div>  int main() {</div><div>    // { constexpr char c = get_first_char(""); }  // compile-time error</div><div>    { constexpr char c = get_first_char("a"); }  // good</div><div>    { char c = get_first_char(""); }  // runtime error handling</div><div>    { char c = get_first_char("a"); }  // good</div><div>  }</div></div><div><br></div><div><br></div><div>I was thinking about using static_assert, which seems hard to integrate into only the compile-time version but not the runtime version.</div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">If that doesn't work, then like you said, you could use a UDL to force the checking at compile time.</div></div>
</blockquote></div></div>