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

Zachary Turner via llvm-dev llvm-dev at lists.llvm.org
Tue Oct 11 21:26:41 PDT 2016


On Tue, Oct 11, 2016 at 8:59 PM Mehdi Amini <mehdi.amini at apple.com> wrote:

>
> 5. *Not flexible.*  How do you print a std::chrono::time_point with
> llvm::format()?  You can't.  You have to resort to providing an overloaded
> streaming operator or formatting it some other way.
>
>
> It seems to me that there is no silver bullet for that: being for
> llvm::format() or your new proposal, there is some sort of glue/helpers
> that need to be provided for each and every non-standard type.
>

I only half agree with this.  for llvm::format() there is no glue or
helpers that can fit into the existing model.  It's a wrapper around
snprintf, so you get what snprintf gives you.   You can go *around*
llvm::format() and overload an operator to print your
std::chrono::time_point, but there's no way to integrate it into
llvm::format.  So with my proposed library you could write:

os << format_string("Start: {0}, End: {1}, Elapsed: {2:ms}", start, end,
start-end);

Or you could write:

os << "Start: " << format_time_point(start) << ", End: "
<< format_time_point(end) << ", Elapsed: " <<
std::chrono::duration_cast<std::chrono::millis>(start-end).count();
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161012/01ea6532/attachment.html>


More information about the llvm-dev mailing list