Apr 11, 2007 16:56
In C, C++, C# and Java, void is kind of a semitype. It can be used as a return type but not as an argument type or value type. I think it would however be possible to extend these languages to make void a type like any other.
Void represents the zero-byte return structure. As a type in an extended language, void would be a zero-byte type, with sizeof(void) = 0. Accordingly, void has 256^0 = 1 value, the void value. Variables declared with type void would therefore take up no space. Setting a void reference would do nothing, while getting would return the void value. Void arguments to functions would likewise take up no argument stack. There should be no interference with the existing use of void*: clearly it's safe for any pointer to be implicitly cast to void*, since set and get operations on void references do nothing.
There is the question of the address of a void variable or argument. Would such variables be nominally "allocated" so that their addresses are non-null? This might be necessary for "vararg" functions with varargs following a void argument.
Why do this? Simply because by eliminating a corner case, it makes polymorphic programming (templates in C++, generics in C# and Java) easier.
software