What is const void?

The description of std::is_void states that:

Provides a constant member value equal to true if T is a type of type, const void, volatile void, or const volatile void.

Then what could be const void , or volatile void ?

This answer states that the return type of the const void type is invalid (however, it compiles on VC ++ 2015)

 const void foo() { } 

If the standard const void invalid (VC is wrong) - then what is const void ?

+76
c ++ c ++ 11 c ++ 14
Jun 17 '16 at 12:09
source share
3 answers

const void is the type with which you can create a pointer. It looks like a regular void pointer, but conversions work differently. For example, a const int* cannot be implicitly converted to void* , but it can be implicitly converted to const void* . Similarly, if you have const void* , you cannot static_cast it to int* , but you can static_cast it to const int* .

 const int i = 10; void* vp = &i; // error const void* cvp = &i; // ok auto ip = static_cast<int*>(cvp); // error auto cip = static_cast<const int*>(cvp); // ok 
+79
Jun 17 '16 at 12:16
source share

As void , const void is a type of void. However, if const void is a return type, const does not make sense (albeit legal!), Because [expr] / 6 :

If prvalue is initially of type “cv T ”, where T is an unqualified non-class, non-array of type cv, expression type is brought to T before any further analysis.

However, it is a valid type and is found, for example. C-standard library functions , where it was used to ensure const-correctness of argument pointers: int const* cannot be converted to void* , but void const* .

+20
Jun 17 '16 at 12:22
source share

Types may be the result of patterns; a template can indicate const T and be created using T as void .

The linked answer is misleading or rather limited due to the fact that it considers a special case of type without a template, and even then const void may be meaningless, but it is valid code.

+17
Jun 17 '16 at 12:16
source share



All Articles