C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Type-safe printf and auto

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Sun, 12 Mar 2023 20:30:28 +0200
On Sun, 12 Mar 2023 at 20:25, Robin Rowe via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> > Have you looked at std::format? It's part of C++20.
>
> Thiago, you make a good point, e.g.:
>
> std::string message = std::format("The answer is {}.", 42);
>
> https://en.cppreference.com/w/cpp/utility/format
>
> There's also std::print in C++23.
>
> https://en.cppreference.com/w/cpp/io/print
>
> A couple issues:
>
> 1. Not practical to rewrite legacy C/C++ code containing a zillion
> printf statements.

You would need to rewrite that code anyway to make use of a new printf format
specifier that performs type deduction.

> 2. Building embedded systems and game servers, we may want to avoid
> std::string due to heap global lock.

You don't need to use a global heap to use std::format. See
https://en.cppreference.com/w/cpp/utility/format/format_to_n

> It's not that we lack more modern approaches for type-safe printing such
> as std::iostream, std::format or std::print. It's that a small change to
> printf could make legacy code maintenance and embedded systems
> development better.

That "small change" doesn't seem to be implementable with the language
facilities
provided by C.

Received on 2023-03-12 18:30:41