William Ahern
3/21/2011 2:59:00 AM
Spiros Bousbouras <spibou@gmail.com> wrote:
> Why does C have void* ? Imagine a C' where the functionality that void*
> provides would be provided instead by unsigned char* plus what
> unsigned char* usually does. In what way would C' be inferior to C ?
In all the same ways that C++ is inferior to C. If his book is to be
believed, Stroustrup designed C++ to stop people from casting so much. The
type-intelligent OOP semantics were the carrot, and the ugly casting
notations--static_cast<>(), et al--were the stick.
But if you read actual C++ code in the wild, it's thick with explicit casts.
In fact, so much so that one would think the design must somehow
fundamentally encourage it.
On the other hand, I rarely see casting in C. Void pointers don't seem to be
abused or used much at all, in my experience. If used at all it's not to
acquire a different type for the reference, but merely for passing generic
objects.
Void pointers are the simple workaround to the caveats of strict typing.
C++'s typing semantics present the complex "solution". Both are an attempt
to provide a safer--compiler instrumented--workaround to strict typing than
explicit casting.