C++ Logo

std-proposals

Advanced search

[std-proposals] Format output-streamable types?

From: Robin Savonen Söderholm <robinsavonensoderholm_at_[hidden]>
Date: Thu, 12 Dec 2024 14:23:04 +0100
Hi!

I enjoy the `format`, `format_to` and `print`-facilities that came with
C++20 and 23 (coming from the fmt-library) but I just noticed that I can't
format e.g. std::bitset. Bitset, however, do have an operator<<() defined
which should be usable in the format context. So I made a very basic,
proof-of-concept streambuf-derived type that could take an output iterator
and wrap it inside streambuf:s API so that it was possible to call the
operator<<() with an arbitrary output (char) iterator. I.e. I could do a
formatter::format-function that just delegated the work to the operator<<().

I do however, wonder if it is best to just ask for the formatter to be
specialised for all types that already have an operator<< (as it may
arguably be slightly faster and removes the need of vtable calls inside the
format function) or if it actually is desirable to have a default
specialisation that says "incase of an
operator<<(basic_ostreambuf<Char...>&, T const&) exist, use it for
formatting". I could also think of a hybrid version where manual
specialisation of std-types are made as well as the iterator-to-stream_buf
type that can be used by users that do not want to re-implement their
type-to-string logic.

Thoughts?

// Robin

Received on 2024-12-12 13:23:17