An incomplete type; it is not possible for an object to have type void
, nor are there arrays of void
or references to void
. It is used as the return type of functions that do not return anything.
Moreover, a function may redundantly be declared with a single parameter of type void
; this is equivalent to declaring a function with no parameters (e.g. int main()
and int main(void)
declare the same function). This syntax is allowed for compatibility with C (where function declarations have a different meaning than in C++).
The type void*
("pointer to void
") has the property that any object pointer can be converted to it and back and result in the same pointer. This feature makes the type void*
suitable for certain kinds of (type-unsafe) type-erasing interfaces, for example for generic contexts in C-style APIs (e.g. qsort
, pthread_create
).
Any expression may be converted to an expression of type void
; this is called a discarded-value expression:
static_cast<void>(std::printf("Hello, %s!\n", name)); // discard return value
This may be useful to signal explicitly that the value of an expression is not of interest and that the expression is to be evaluated for its side effects only.