[comp.lang.c] void and typedef

leichter@yale.UUCP (Jerry Leichter) (01/13/87)

Is the following legal ANSI C?

	typedef void empty;

I've tried several existing C compilers, including VAX C and a random Unix C,
and none accept it.  However, no existing documentation says this is illegal,
and the ANSI spec is no better:  It says that no "object" may have type void,
and that there are no values of type void, but neither applies here.  This
kind of declaration could be useful for documentation purposes, for disting-
uising among different kinds of functions that don't return values - e.g.,
those that exit, or that return no value but might signal one, etc.  It can
also be useful in machine-generated code:  A C pre-compiler I'm working on
extends the C type system in certain limited contexts by allowing for the
introduction of new, distinct types.  I have a "newtype" storage class which
is the same as typedef, except that instead of introducing a synonym, it
creates a new type.  The result of the pre-compiler is C code in which no
new types appear.  In general, newtype is simple turned into typedef.  How-
ever, for what I'm doing it turns out to be very useful to allow for alterna-
tive "void" types.  Currently, I have to special-case such code and typedef
to int instead of void.  (Beyond pain in the compiler, this doesn't matter
since I still have no "objects" - things that exist at run time - with a void
type.)

It's interesting that the following IS accepted by all compilers I've tried:

	typedef void vf();

While it's possible to come up with uses for vf, they are pretty artificial.

If the definition of "empty" above is to be considered acceptable in ANSI C,
a bunch of related questions suggest themselves:

	- In a function declaration with an explicitly empty argument list,
		is "void" a datatype or is it just a piece of syntax?  That
		is, is:

			typedef void empty;
			f(empty)
			{ ... }

		the same as:

			f(void)
			{ ... }

	- Similarly, is the "void *" datatype a special case, or is "empty *"
		legal and "the same thing"?

	- Is:
			typedef void *ADDRESS;

		legal, and does it do what its name implies?

My own feelings on this matter are:

	There is no ambiguity or special difficulty imposed by allowing
		synonyms for the void type, so they SHOULD be allowed, since
		(a) they can be useful and (b) why add another special case?

	(void *) should act as a real data type, (empty *) should be the same
		thing, and typedef void *ADDRESS; should be allowed and create
		a name for the "anonymous pointer" type.

	The "void" in function declarations should ideally be viewed as just
		a piece of syntax, but it is probably a lot easier on compiler
		writers to allow any synonym for void in this context, as this
		allows for common "read a type name, return underlying type"
		code to be used.

							-- Jerry