typeof -- another use.
Kevin Martin
kpmartin at watmath.UUCP
Sat Jul 21 04:30:13 AEST 1984
When I was proposing alignof(type or expr) is was also considering typeof,
but my justification seemed weak. Here is what I want it for. Once again,
related to varargs stuff. In the Berkelese <varargs.h> stuff, you eventually
prime a variable to point to your first argument, and another macro returns
you the next arg and bumps the pointer. Suppose that, having stripped off the
first argument (say, an error severity level for your error message printer),
you now want to pass *all the rest of your args* to (say) printf. The way
I suggest is to add another piece to the Berkeley stuff, namely:
va_apply( func, argptr, type )
(this is how we currently implement it). 'argptr' is one of those arglist
pointers. 'func' is the function to call. 'type' is the type it returns.
This macro expands to:
(*(type (*)())&_apply)( (void *)func, argptr )
That is, call '_apply' as if it were a function returning 'type'. Needless
to say, _apply is *NOT* written in C! It has to build an arglist somewhere from
'argptr', and enter the supplied function, after obliterating itself from the
stack...
The problems are:
1) The user shouldn't have to supply the type, after all, he has already
declared 'func'.
2) The supplied type cannot be a compound type, or else (type (*)()) gives
a syntax error (or the wrong type).
The solution: With 'typeof', the macro expands to:
(*(typeof func *)&_apply)( (void *)func, argptr )
Presto!
Note that 'typeof func' gives 'function-returning-blart', not 'pointer-to-
function-returning-blart'. The second implementation also detects the use
of a 'func' that isn't a function, since the 'typeof' operator supplies the
function-hood, rather than a macro blindly casting it on.
More information about the Comp.lang.c
mailing list