Apparently, it is possible to declare a function returning const void
:
const void foo() { }
g++ seems to consider the const
important, because the following code does not compile:
#include <type_traits> static_assert(std::is_same<void(), const void()>::value, "const matters");
So does const void
have any practical significance?
const void is a type which you can form a pointer to. It's similar to a normal void pointer, but conversions work differently. For example, a const int* cannot be implicitly converted to a void* , but it can be implicitly converted to a const void* .
Void functions, also called nonvalue-returning functions, are used just like value-returning functions except void return types do not return a value when the function is executed. The void function accomplishes its task and then returns control to the caller. The void function call is a stand-alone statement.
The const keyword allows you to specify whether or not a variable is modifiable. You can use const to prevent modifications to variables and const pointers and const references prevent changing the data pointed to (or referenced).
The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.
Not really. But to ignore cv-qualifications on void or to make them errors could create unnecessary complexity in terms of both compiler implementation and end-user code. Consider templates like
template<typename T> const T ...
There's no reason to make using void in that scenario a special case (more than it already is), it would just create headaches.
Also, while const void
isn't helpful, const void*
has its uses.
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