News for hackers. Stuff that matters.
Re:Pprint: Pretty Printer for Modern C++ by rwj on Monday April 29, 2019 @03:55PM (#19782000)
The library {fmt} (http://fmtlib.net/latest/index.html) is on track to be included in the standard.
Re:Pprint: Pretty Printer for Modern C++ by arcticbull on Monday April 29, 2019 @08:42PM (#19784064)
Wow, 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.

Re:Pprint: Pretty Printer for Modern C++ by 0db532a0 on Tuesday April 30, 2019 @04:53AM (#19786195)
Why? I think there’s a reason behind it. I always imagined it to to be shifting bits along the stream.
Re:Pprint: Pretty Printer for Modern C++ by arcticbull on Tuesday April 30, 2019 @04:58AM (#19786221)
It's about violating the principle of least surprise. If you create a culture of redefining any and all operators all the time to effectively create DSLs, you can't infer what anything does. You can even override operator, for some reason. Just looking at a piece of code you have no idea what it's actually intending to do.
Re:Pprint: Pretty Printer for Modern C++ by 0db532a0 on Tuesday April 30, 2019 @05:30AM (#19786367)
I agree with you that there are some DSLs which are very difficult to understand at first look. However, to me shifting, and pushing/pulling over streams are not so dissimilar.

There are lots of cases where overriding operators makes sense and is intuitive to the reader, for instance when you are dealing with monoids, groups or rings.

Re:Pprint: Pretty Printer for Modern C++ by arcticbull on Tuesday April 30, 2019 @07:59PM (#19793240)
The issue is not that each one in isolation is particularly problematic, the issue IMO is when they get assembled by an end-user into a pile of mixed metaphors.

  fmt::memory_buffer out1;
  std::ostream out2 = std::cout;
  auto out3 = printer(stream);
  auto out4 = my_printer();

  format_to(out1, ...)
  out2 << "Hi..."
  out3.print(5);
  out3 += 6;
These all do the same but it's a total mess.
Re:Pprint: Pretty Printer for Modern C++ by 0db532a0 on Wednesday May 01, 2019 @04:22AM (#19795396)
Anyone who writes like that needs to have a chat with Uncle Bob.
Re:Pprint: Pretty Printer for Modern C++ by arcticbull on Wednesday May 01, 2019 @04:46AM (#19795492)
Haha, that's fair :) I'm trying to convey that sometimes as an end developer, you pick a number of third party libraries based on what they do -- not what their API definition looks like. Then you end up in an awkward mix-n-match of DSLs that are hard to reason about without a deep knowledge of each because the operators you normally key off of are redefined. Maybe thats why I'm not a C++ developer haha.
Re:Pprint: Pretty Printer for Modern C++ by dgellow on Monday April 29, 2019 @04:21PM (#19782232)
Oh that’s great! Where have you seen that it might be included in the standard?
Re:Pprint: Pretty Printer for Modern C++ by dasloop on Monday April 29, 2019 @08:27PM (#19783976)
Has been presented to the committee, first steps in any case. The proposal: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p064...
Re:Pprint: Pretty Printer for Modern C++ by dgellow on Tuesday April 30, 2019 @08:21AM (#19787245)
Interesting, 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!
Re:Pprint: Pretty Printer for Modern C++ by vitaut on Wednesday May 01, 2019 @10:49AM (#19797479)
A lot of work has been done since 2017: the proposal is at its 9th revision (http://fmtlib.net/D0645R9.html) and is prioritized (but not accepted yet) for C++20.
Re:Pprint: Pretty Printer for Modern C++ by dgellow on Wednesday May 01, 2019 @03:34PM (#19800784)
Awesome, thank you for sharing
Re:Pprint: Pretty Printer for Modern C++ by vitaut on Wednesday May 01, 2019 @10:53AM (#19797526)
FWIW {fmt} already supports formatting of containers, tuples and ranges as of version 5.0 (https://github.com/fmtlib/fmt/releases/tag/5.0.0), for example:

  #include <fmt/ranges.h>

  std::vector<int> v = {1, 2, 3};
  fmt::print("{}", v); // prints {1, 2, 3}
Re:Pprint: Pretty Printer for Modern C++ by m0zg on Tuesday April 30, 2019 @01:52AM (#19785454)
This reminds me very strongly of Google's string::Substitute(). Super convenient, and IIRC it was faster than sprintf as well.

Looks like it's now public as a part of Abseil: https://github.com/abseil/abseil-cpp/blob/bf29470384a101b307...

Re:Pprint: Pretty Printer for Modern C++ by calebzulawski on Monday April 29, 2019 @03:06PM (#19781555)
I also recently created a pretty printer library (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.

Re:Pprint: Pretty Printer for Modern C++ by phaedrus on Monday April 29, 2019 @05:24PM (#19782750)
I like your idea to support separately tracked indentation levels.
Re:Pprint: Pretty Printer for Modern C++ by calebzulawski on Monday April 29, 2019 @06:01PM (#19783004)
Thanks! It really makes writing printers very easy.
Re:Pprint: Pretty Printer for Modern C++ by saagarjha on Monday April 29, 2019 @02:52PM (#19781403)
I 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?
Re:Pprint: Pretty Printer for Modern C++ by phaedrus on Monday April 29, 2019 @05:16PM (#19782689)
A 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.

Re:Pprint: Pretty Printer for Modern C++ by rsynnott on Monday April 29, 2019 @07:24PM (#19783590)
Yeah, 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.
Re:Pprint: Pretty Printer for Modern C++ by kabdib on Monday April 29, 2019 @10:05PM (#19784493)
They 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.

Re:Pprint: Pretty Printer for Modern C++ by dearrifling on Tuesday April 30, 2019 @05:52AM (#19786478)
ADL is important for customization points in modern C++.
Re:Pprint: Pretty Printer for Modern C++ by colejohnson66 on Monday April 29, 2019 @04:44PM (#19782434)
Does anyone know why C++ uses the shift operator for streams? It’s always rubbed me the wrong way
Re:Pprint: Pretty Printer for Modern C++ by phaedrus on Monday April 29, 2019 @05:12PM (#19782655)
Originally 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.

Re:Pprint: Pretty Printer for Modern C++ by rand84545 on Monday April 29, 2019 @06:13PM (#19783084)
I mean, it's also looks intuitive. if you imagine the arrows as the data fllowing.
Re:Pprint: Pretty Printer for Modern C++ by mikepurvis on Monday April 29, 2019 @05:21PM (#19782725)
Among 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.
Re:Pprint: Pretty Printer for Modern C++ by Ace17 on Tuesday April 30, 2019 @02:21AM (#19785564)
phaedrus 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";

Re:Pprint: Pretty Printer for Modern C++ by JoeAltmaier on Monday April 29, 2019 @04:51PM (#19782506)
Yes! 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.
Re:Pprint: Pretty Printer for Modern C++ by eMSF on Monday April 29, 2019 @03:01PM (#19781510)
My 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

Re:Pprint: Pretty Printer for Modern C++ by a_t48 on Monday April 29, 2019 @04:39PM (#19782388)
Efficiency, 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.

Re:Pprint: Pretty Printer for Modern C++ by kccqzy on Monday April 29, 2019 @05:04PM (#19782606)
This 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.
Re:Pprint: Pretty Printer for Modern C++ by tommikaikkonen on Tuesday April 30, 2019 @01:46AM (#19785434)
Indeed. 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

Re:Pprint: Pretty Printer for Modern C++ by flo123456 on Tuesday April 30, 2019 @01:20AM (#19785356)
There’s also https://github.com/tfc/pprintpp which is quite nice.
Re:Pprint: Pretty Printer for Modern C++ by kccqzy on Monday April 29, 2019 @05:14PM (#19782678)

    pprint::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.