C++ Void non-pointer-Collection of common programming errors
In a strongly typed language, all data has a type, so there is no concept of “void” as a datatype. In C and C++, it’s meanings are either “no data” (as a return value), or “data whose type you don’t know” (as a pointer target).
You are proposing an in-between state of “data whose type you don’t know but whose size you do”, presumably as an opaque version of a type you pass by value. Besides being rather a dangerous thing to do (since you have to manually update all the code using the opaque type when you change the size of the real type), this can already be done without adding weird extensions to the language, for example:
struct void8 { char bytes[8]; };
This could be used in conjunction with reinterpret_cast
to pass POD types around as opaque lumps, in much the way you suggest. Using it with non-POD types is even more likely to invoke undefined behaviour than using void*
to refer to non-POD types.
It would be safer and more idiomatic to handle opaque types as pointers or references to named types that have been declared but not defined, or something like boost::any
if you want value semantics. There’s rarely a need to use “void” as a placeholder type in C++.
Originally posted 2013-11-09 19:42:20.