rakzz
rakzz

Reputation: 213

Why can't objects of type void be created in C++?

C++ doesn't allow creating objects of type void. Why is that?

Upvotes: 15

Views: 4213

Answers (7)

In silico
In silico

Reputation: 52217

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).

Upvotes: 13

Tony Delroy
Tony Delroy

Reputation: 106246

void is a placeholder indicating no object of any type is expected.

As a function argument specification

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.

As a function return type

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?

Comparison with other types

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.

pointers to void

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.

Upvotes: 3

Yakov Galka
Yakov Galka

Reputation: 72539

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.

Upvotes: 4

liaK
liaK

Reputation: 11648

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.

Upvotes: 12

Matthieu M.
Matthieu M.

Reputation: 300399

It is, simply, an arbitrary decision, from C.

In C, all types (except from void) are used to carry a value. void, by definition, does not hold any value. The language designers therefore decided it would not be possible to instantiate it.

C++ takes after C, and the decision remained. Otherwise it would have been necessary to define a storage size (probably the same as bool).

In C++ it is indeed annoying, especially because of the special casing necessary for template classes / functions, but no-one deemed it worthy of modification since it's possible to specialize the templates for void and thus it's not blocking.

Upvotes: 1

Sat
Sat

Reputation: 1626

void represents Nothing.Even you are creating function with Void type we no need to return a value.Same like Here also No object of type void is declared.

Upvotes: 1

Mahesh
Mahesh

Reputation: 34665

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.

Upvotes: 2

Related Questions