Zachary Turner via llvm-dev
2016-Oct-12 04:26 UTC
[llvm-dev] RFC: General purpose type-safe formatting library
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>
Pavel Labath via llvm-dev
2016-Oct-12 09:33 UTC
[llvm-dev] RFC: General purpose type-safe formatting library
On 12 October 2016 at 05:26, Zachary Turner via llvm-dev <llvm-dev at lists.llvm.org> wrote:> os << format_string("Start: {0}, End: {1}, Elapsed: {2:ms}", start, end, > start-end);What would happen if I accidentally type "ps" instead of "ms" (I am assuming we will not support picoseconds here)? Will this abort at runtime? I would prefer if *all* arguments to the format were checkable at compile time: I.e. something like: os << "blah blah" << format<std::milli>(end-start) << "blah blah"; I understand this may clash a bit with the desire for a compact representation, but maybe with some clever design we could achieve both? pl
Zachary Turner via llvm-dev
2016-Oct-12 13:11 UTC
[llvm-dev] RFC: General purpose type-safe formatting library
In my current implementation, it's up to the format provider. If you have an illegal format spec (eg {0;0}) it ignores it and prints the format spec as a literal. We could also add an assert here in theory. If/when we move to c++14, a constexpr StringRef implementation would allow us to parse and validate the entire format string at compile time. Since conciseness is one of the main goals of a library such as this, I would hate to actively hamper this for more compile time checking. If you write an invalid format spec presumably your test will fail since it will ignore it On Wed, Oct 12, 2016 at 2:34 AM Pavel Labath <labath at google.com> wrote:> On 12 October 2016 at 05:26, Zachary Turner via llvm-dev > <llvm-dev at lists.llvm.org> wrote: > > os << format_string("Start: {0}, End: {1}, Elapsed: {2:ms}", start, end, > > start-end); > > What would happen if I accidentally type "ps" instead of "ms" (I am > assuming we will not support picoseconds here)? > > Will this abort at runtime? > > I would prefer if *all* arguments to the format were checkable at compile > time: > I.e. something like: > os << "blah blah" << format<std::milli>(end-start) << "blah blah"; > > I understand this may clash a bit with the desire for a compact > representation, but maybe with some clever design we could achieve > both? > > pl >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161012/61834a37/attachment.html>