12 ms·
Pprint: Pretty Printer for Modern C++
- saagarjha 7y agoI find it interesting that this library requires the creation of a "printer" object instead of the (IMO more standard) definition of operator<<. Is there any specific reason why the author chose to go this way?
- eMSF 7y agoMy guess is it's mimicking a Python library of similar name and purpose. edit: the similarities are pretty obvious, but for those too lazy to Google, see https://docs.python.org/3/library/pprint.html https://docs.python.org/3/library/pprint.html
- a_t48 7y agoEfficiency, likely. Using standard function call syntax lets you know all arguments up front. Using the stream operator doesn't. There's likely a way to do it with templates, but this is easier. Edit: this also lets them assert on argument types.
- colejohnson66 7y agoDoes anyone know why C++ uses the shift operator for streams? It’s always rubbed me the wrong way
- JoeAltmaier 7y agoYes! They look cute, sure. But aren't they the wrong precedence? I think its because its a little-used operator that can be overloaded without interfering with normal expressions within the stream I/o invocation.
- phaedrus 7y agoOriginally C++ lacked templates, and even once it got templates it was 20 (?) years before it got variadic templates. Operator overloads were added early, however, so using an operator for this was a "hack" to make a type safe print function. (It probably also served as a demo application for operator overloading.) If you squint enough, it looks like a fluent interface, just fluent interfaces wouldn't become mainstream until later. That is, "cout << a << b << c;" is pretty close to "cout.print(a).print(b).print(c);" Another reason to use an operator is that if you use free functions (without variadic parameters) then your parens and nesting stacks up: "print(print(print(cout, a), b), c);" An infix operator doesn't have that problem. In modern C++, there is at least the possibility to replace "<<" with something better for printing.
- rand84545 7y agoI mean, it's also looks intuitive. if you imagine the arrows as the data fllowing.
- mikepurvis 7y agoAmong its many flaws, you can't really use streams for output where internationalization will ever be a consideration, which immediately eliminates many potential use cases.
- Ace17 7y agophaedrus explained why use an infix operator, but why specially the shift operator? My guess: because it has very low precedence, allowing stuff like: > cout << a + b * c << " is the result\n"; Instead of: > cout << (a + b * c) << " is the result\n";
- phaedrus 7y agoA huge problem with cout and << is that setting flags (such as to print numbers in hex) is global and a destructive update. If you don't save the stream state beforehand and restore it you change the flags for printing in unrelated code. So making an object for printing probably helps with scope issues like that.
- rsynnott 7y agoYeah, I always wondered was << just an ill advised attempt to show off operator overloading (I’m sure things had it before C++, but it definitely wasn’t commonplace). It never made much sense to me.
- kabdib 7y agoThey were adding features to the language (e.g., "Koenig lookup") in the CFront era to accommodate iostreams. I was sad that they were spending a bunch of resources on that relatively useless stuff, rather than a sorely needed set of standard collection classes, not to mention a string class. And by "standard" I don't mean actual standards -- it was far too early in the game for that -- but even just enough to get by.
- dearrifling 7y agoADL is important for customization points in modern C++.
- calebzulawski 7y agoI also recently created a pretty printer library (https://github.com/calebzulawski/hippo https://github.com/calebzulawski/hippo). In some ways they are similar, though I felt it was valuable to avoid using ostreams at all, and store indentation levels separately from the printed string. Additionally, I think reflection macros are especially helpful for creating printers for user-defined types.
- phaedrus 7y agoI like your idea to support separately tracked indentation levels.
- calebzulawski 7y agoThanks! It really makes writing printers very easy.
- rwj 7y agoThe library {fmt} (http://fmtlib.net/latest/index.html http://fmtlib.net/latest/index.html) is on track to be included in the standard.
- dgellow 7y agoOh that’s great! Where have you seen that it might be included in the standard?
- dasloop 7y agoHas been presented to the committee, first steps in any case. The proposal: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0645r1.html http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p064...
- dgellow 7y agoInteresting, thank you for sharing. Nothing new since 2017 if I understand correctly? I understand that the C++ proposal system is very slow, still it would be nice to know if that’s still something people are working on. I would personally love to see something like this in the standard!
- arcticbull 7y agoWow, this is so much better. I hope they deprecate cout and its inane operator overloads. Call me old-fashioned but I like an operator to do something at least logically consistent with its intended use. Shifting shouldn't print or read from a stream, what on earth made them think that's a good idea? One thing that drives me crazy about C++ is that if you incorporate a few different third-party libraries you often end up writing a few different DSLs. Everyone has their own subset of operator overloads that do different things in different contexts and you just have to remember which one does what based on your receiver type -- which is often 'auto' these days anyways. It's maddening. Worse still, the STL plays this game. Time to move on.
- kccqzy 7y agoThis is a bit like '%r' or repr() in Python, the Debug trait in Rust, or the Show class in Haskell, but it doesn't really address what most pretty printers need: highly customized formatting, as used in Wadler/Leijen pretty printers.
- tommikaikkonen 7y agoIndeed. I wrote a Python pretty printer that uses a Wadler/Leijen algorithm, and it's a great fit. It doesn't take too long to learn how to use the layout primitives, and it allows for easy creation of higher level abstractions while still maintaining plenty of control. https://github.com/tommikaikkonen/prettyprinter https://github.com/tommikaikkonen/prettyprinter
- kccqzy 7y agopprint::PrettyPrinter printer; printer.print("x'x\"x"); And the output is "x'x"x" So it doesn't actually escape any characters in the string. Same with newlines inside the string.
- flo123456 7y agoThere’s also https://github.com/tfc/pprintpp https://github.com/tfc/pprintpp which is quite nice.