News for hackers. Stuff that matters.
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.