Give type as argument like va_arg in C

c, types, variadic-functions

Solution

Notice that va_arg cannot determine the actual type of the argument passed to the function, but uses whatever type is passed as the type macro argument as its type.

Lets see how `va_arg` works, one possibility from here, is

#define va_arg(ap,t) (*(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)))

First of all, it's a MACRO not a function.

If you want write a type base MACRO as same as `var_arg`, you have two main tools, `pointers` and `sizeof`.

You can increase/decrease/lookup a pointer with `sizeof` steps.

Problem

`va_arg(a,type)` macro expands to `__builtin_va_arg(a,type)` which is built-in part of compiler. Is it possible to implement type based functionality, if yes how?

Original source