Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site watmath.UUCP Path: utzoo!watmath!kpmartin From: kpmartin@watmath.UUCP (Kevin Martin) Newsgroups: net.lang.c Subject: typeof -- another use. Message-ID: <8397@watmath.UUCP> Date: Fri, 20-Jul-84 14:30:13 EDT Article-I.D.: watmath.8397 Posted: Fri Jul 20 14:30:13 1984 Date-Received: Sat, 21-Jul-84 03:31:28 EDT References: <4440@edai.UUCP> Reply-To: kpmartin@watmath.UUCP (Kevin Martin) Organization: U of Waterloo, Ontario Lines: 33 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 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.