C++ doesn't allow creating objects of type void
. Why is that?
The void type, in several programming languages derived from C and Algol68, is the return type of a function that returns normally, but does not provide a result value to its caller.
You can't declare a variable of type void .
A reference says, "I refer to something that is of this type." Allowing void or null reference would weaken that difference from pointers. Granted, it's still possible for a reference to refer to an object that doesn't exist anymore, but that is an exception.
Consider the code below.
class Foo
{
// etc ...
};
int main()
{
// Declaring an object...
Foo foo;
// foo has been created.
// etc ...
return 0; // When function returns, foo will be destroyed.
}
In order to know how to actually create the object, the compiler has to know the type. Informally, you can think of void
as a "type" representing an absence of type. Therefore, the compiler can't possibly know how to create a void
object. You can't create an object you don't know how to create.
int main()
{
void v; // "I don't know how to create this!"
}
That being said, there are other cases where void
makes sense. For example, a void
function has no return value. You can't assign a type to things (like return values) that do not exist.
You can also have a pointer to void
, or void*
. Unlike the plain void
, void*
is valid and simply means "a pointer to an object of some unknown type". Obviously you can't do much with a void*
until you cast it into an actual, known type (assuming the cast is safe, of course).
It is because void
is an incomplete type.
From Standard docs., incomplete types 3.9 states that,
5
A class that has been declared but not defined, or an array of unknown size or of incomplete element type, is an incompletely-defined object type.38) Incompletely-defined object types and the void types are incomplete types (3.9.1). Objects shall not be defined to have an incomplete type.38) The size and layout of an instance of an incompletely-defined object type is unknown.
Since void
is an incomplete type, it's size and layout cannot be determined and hence it cannot be defined.
As a side note, you can create a temporary of type void
:
void f()
{
return void();
}
void g()
{
return cout << "hi", f();
}
this is valid code. It is extremely useful in generic code. It was even considered to allow the usage of built in types (including void) in some places like base classes:
template<class T> class A : T { };
A<string> x; // works
A<int> y; // error, but could be usefull
A<void> z; // error, but could be usefull.
void
is a placeholder indicating no object of any type is expected.
Historically C used an empty argument list in a function declaration ala return_type f();
to allow f()
to be called with however-many and whatever-type arguments were specified at each call site, whereas return_type f(void);
made it explicit no arguments were expected or allowed. I.e. C was prepared to trust the programmer to get the number and types of arguments right, with any mistake likely to corrupt data and/or crash the program.
There woud also be some ambiguities in the language if void
wasn't there to establish the overall "type variable|function" sequence that's part of the grammar of the language. For example:
f(); // declaration of a function that returns nothing?
// OR a function call?
It is not really a data type itself in the sense of representing some interpretation of an area of memory, as int, float, char etc., classes, unions etc. all do.
For void*
, it indicates a loss of insight into the contents of memory at the contained address, such that sometime before dereferencing the pointer it must be cast to a specific type reflecting the bitwise layout of data at that memory address. Then, the compiler can interpret and operate on that bit layout in accord with the then-known data type.
In C++
, every thing can be related to object. So, when said -
void variable ;
How many bytes must the platform allocate for variable
with out knowing it's type. Is it int
or float
or double
or any other primitive type. So, it is not allowed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With