C++ Logo


Advanced search

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

From: Alejandro Colomar (man-pages) <"Alejandro>
Date: Mon, 13 Mar 2023 11:24:40 +0100
On Mon, Mar 13, 2023, 03:44 Robin Rowe via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> > Well, the problem there is that the compiler knows that the format
> > string is a compile-time constant, but the implementation of printf
> > doesn't.
> Good catch, Ville. If I understand your point...
> unsigned i = 0;
> printf("%d",i);// Warning: use %u
> const char* format = "%d";
> printf(format,i);// Any warning?

Yes, there will be a warning about the format not being a string literal.

> If our concern is the second case, or heaven forbid when reading format
> from a file, that the compiler will not know enough, you're right. I
> don't expect the compiler to catch that and am not suggesting the printf
> interpreter should catch it at runtime.
> I'm only suggesting that the compiler, when given "%?" in the first
> printf call above, be able to replace it silently with "%u". However, if
> the format isn't a string constant in the printf call, then the format
> string must be fully specified.
> In the error case that the printf interpreter receives a "%?" through
> this hole, the behavior could be to print a message such as "printf %?
> format error", as printf at runtime doesn't know what is packed in
> variargs.
> Appreciate the feedback.
> Robin
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-03-13 10:24:53