Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of const void?

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?

like image 491
fredoverflow Avatar asked Feb 20 '11 15:02

fredoverflow


People also ask

What does const void mean?

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

Why void is useful?

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.

What is the point of const variables?

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

What is the purpose of a const?

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.


1 Answers

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.

like image 51
Logan Capaldo Avatar answered Oct 13 '22 04:10

Logan Capaldo