Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

void — a type with no values

void is a fundamental type with no values, used primarily as the return type of a function that returns no value. Objects of type void cannot be created. The separate type void* can hold object pointers without retaining their pointed-to type information.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

void f();
void f(void);
void* p = nullptr;
static_cast<void>(expression);

void is a fundamental type with an empty set of values. It is an incomplete type that cannot be completed; this also applies to its const- and volatile-qualified versions. It does not mean zero or an empty object. The declaration void value; is invalid.

A void return type means that calling the function supplies no result value, although the function can perform actions, such as modifying an argument passed by reference. In an ordinary function of this type, return; ends execution; reaching the end of the body has the same effect. You can also write return expression; if expression has type void, but not return 42;.

In C++, the declarations void f(); and void f(void); both declare a function with no parameters. The lone, unnamed void here is special notation for an empty parameter list, not a parameter that can receive a value.

void* is a pointer type, not void itself. An object pointer can be implicitly converted to void* provided this does not remove qualifiers; use const void* for a const object. Converting back to the original pointer type requires an explicit conversion, such as static_cast<int*>(address). The conversion performs no dynamic type check: before accessing the object, you must ensure the correct type and the validity of the pointed-to object. In the example, address comes from the address of an int object that is still alive.

The expression static_cast<void>(expression) explicitly discards the result without skipping evaluation or its side effects. This entry targets C++23; the supplied source is an evolving working draft, not a frozen C++23 edition.

Examples

#include <iostream>

void increment(int& value) {
    ++value;
}

int main() {
    int value = 41;
    increment(value);
    void* address = &value;
    int* restored = static_cast<int*>(address);
    std::cout << *restored << '\n';
}

Result

42

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources