C++ Logo

sg16

Advanced search

Re: [isocpp-lib-ext] LEWG(I) Weekly review - P2549R0: Should the output of std::print to a terminal be synchronized with the underlying stream?

From: Zhihao Yuan <zy_at_[hidden]>
Date: Mon, 28 Mar 2022 22:24:36 +0000
On Monday, March 28th, 2022 at 2:18 PM, Tim Song via Lib-Ext <lib-ext_at_[hidden]> wrote:

> In one of our codebases, where formatting started with iostreams and gradually shifted to {fmt}, there are lots of code of the form:
>

> struct A {int a; int b;friend std::ostream& operator<<(std::ostream& os, A const& a) {fmt::print(os, "{{a={}, b={}}}", a.a, a.b);return os;}};
>

> This seems like a perfectly valid way to implement operator<< to me - except that it doesn't work if fmt::print (and now std::print) can sometimes bypass things in os's buffer. With iostreams in particular, << naturally encourages writing partial lines:
>

> A a{2, 4};std::cout << "A is " << a << '\n'; // might print "{a=2, b=4}A is "

Looks unsurprising to me if it prints"A is {a=2, b=4}"; the use of ostreamoverload of std::print has opt-in thestreams mechanism. This guaranteelooks more motivating that the casediscussed in P2539.

--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
_______________________________________________

Received on 2022-03-28 22:24:39