C++ Logo


Advanced search

Error handling with std::print(std::ostream&,...) (P2093)

From: Dimitrij Mijoski <dim.mj.p_at_[hidden]>
Date: Thu, 03 Feb 2022 23:53:00 +0100
First I'll say that fmt::print(ostream&) does the right thing and just
calls ostream.write.
But in the paper wording, things arent exactly clear.

The paper says

> void vprint_nonunicode(ostream& os, string_view fmt, format_args args);
> 4 Effects: Writes the result of vformat(os.getloc(), fmt, args) to os.
> Throws: As specified in [format.err.report] or system_error if a call
> by the implementation to an operating system or other underlying API
> results in an error that prevents the function from meeting its
> specifications.

Iostreams have their own error handling mechanisms with their 3 error
flags. They can even throw exceptions if one opts-in. All IO functions
follow the same convention regarding these flags, and std::print that
accepts ostream should probably follow
it. https://en.cppreference.com/w/cpp/named_req/UnformattedOutputFunction
The easiest way is to just call ostream.write() like in fmtlib.

I'm writing this because with the current wording I get the impression
that system_error should be thrown if failbit gets set (or even if it
was set before the call to std::print). That exception should be used
only when some C APIs return error like WriteConsoleW() on Windows.

Received on 2022-02-03 22:53:04