Typeof operator in C (Re: An Interesting View of "Strong" Vs. "Weak" Typing)

Gordon Burditt gordon at sneaky.UUCP
Mon Jan 15 16:23:47 AEST 1990


>I said... what would typeof() return for some complex structure. This
>being the typeof that returns a first-class object, not the typeof from
>GCC 
...
>You mean it would return a string? That's an interesting idea. I would have
>expected it to return a small integer of some sort. You know: _T_INT,
>_T_CHAR, _T_DOUBLE.

Ok, how about this extreme:  you pass this function a string,
the type of an argument (equivalent to typeof(typeof(anything)) ), and a 
pointer to that type:

typedef typeof(typeof(int)) type;
void errmsg(char *string, type argtype, void * argptr)
{
	switch(argtype)
	{
	/* How many of these should get "duplicate case value"? */
	case typeof(register char):
	case typeof(register signed char):
	case typeof(register unsigned char):
	case typeof(volatile register char):
	case typeof(volatile register signed char):
	case typeof(volatile register unsigned char):
		die("You can't pass a pointer to a register variable, dummy");
	case typeof(char): 
	case typeof(signed char):
	case typeof(unsigned char):
	case typeof(volatile char):
	case typeof(volatile signed char):
	case typeof(volatile unsigned char):
		.....
	case typeof(char *):
	case typeof(register char *):
	case typeof(unsigned char *):
	case typeof(signed char *):
		.....
	case typeof(char [1]):
	case typeof(char [2]):
		.....
	case typeof(struct bar):
		.....
	case typeof(typeof(int)):
		.....
	default:
		.....
	}
	....
	return;
}

You also get a number of compile-time or run-time functions that take a type 
and return a type:

pointerto(type) == the type of a pointer to type
functionreturning(type) == the type of a function returning type
deregisterize(type) == type with all "register" qualifiers removed
devolatileize(type) == type with all "volatile" qualifiers removed
basetype(type) == type with all "array of", "function returning", and
	"pointer to" removed.

Intel processors, will, of course, need Fat, Skinny, Obese, Normal, and
Anorexic sizes for the result of typeof().

					Gordon L. Burditt
					...!texbell!sneaky!gordon



More information about the Comp.lang.c mailing list