C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sun, 12 Mar 2023 15:29:05 -0700
On Sunday, 12 March 2023 14:40:38 PDT Ray Gardener via Std-Proposals wrote:
> A solution could be a func decl keyword "typed" for funcs that take
> variadic arguments. The compiler then includes type info for each such arg.
> The function's use of va_list (probably a new kind like va_typed_list) then
> lets it access that info.

How? Sounds like a good idea... except we can already do it with C++ variadic
templates and std::any (and arrays thereof). Even if you don't like std::any,
you can pass an array of pointers to the parameters and an array of type_info
to the types of the parameters to your function.

There's no need to modify the compiler further; C++11 already did that.

Now, if you want to do that in C, then you need to either talk to the C
committee or argue for a set of compiler extensions. I think you can do it
with GCC, with __builtin_va_arg_pack_len(), variable-length arrays, and a
_Generic-based replacement for typeid. Proof left as an exercise to the reader
(because I have no interest in tangling with those problems in C, when a
perfectly working C++ solution exists).

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-03-12 22:29:07