C++ Logo


Advanced search

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

From: Robin Rowe <robin.rowe_at_[hidden]>
Date: Sun, 12 Mar 2023 19:44:44 -0700
> 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?

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.


Received on 2023-03-13 02:44:48