简体   繁体   中英

What's the difference between void and cv void?

I have come across the type "cv void" in the latest draft of the C++ standard ( N4606 ) :

8.3.3 [dcl.mptr], paragraph 3

A pointer to member shall not point to a static member of a class (9.2.3), a member with reference type, or “cv void” .

With a little bit of research, I found "cv void" is a real type , but I have no idea what's the difference compared to the type void . Can you explain it with an example (maybe with a code) ?


EDIT :

  • I sort of expected cv would stand for cv-qualified. My question here is, why do we need to "cv-qualify" the type void?
  • The reason I said "cv void is a real type" is that, the standard actually defined it:

    3.9.1 [basic.fundamental], paragraph 9

    A type cv void is an incomplete type that cannot be completed; such a type has an empty set of values...

"cv void" is not a real type. "cv" here is a shorthand for "possibly cv-qualified", which means "may have a const or a volatile on it".

The passage means that a pointer-to-member may not point to an object of the following types: void , const void , volatile void and const volatile void . It's fairly obvious, since such objects cannot exist in the first place, but I guess it's nice to be clear.

We don't "need" to allow void to be cv-qualified. However, it's simpler to allow it than it is to make a special exception forbidding it.

But it does actually have an important practical use: allowing void to be cv-qualified allows us to write cv-correct code with pointers-to- void .

"cv void " means void which is optionally const - or volatile -qualified. Simply void is not so qualified.

And for some reason, someone obviously thought it was a good idea to forbid that for member-pointers (in contrast to normal pointers).
Seems someone is no fan of untyped memory...

why do we need to " cv -qualify" the type void ?

Same reason you would need to cv -qualify any other type. For instance, memcpy 's signature is:

void* memcpy( void* dest, const void* src, std::size_t count );

The src argument is a pointer to const void , because the memory pointed to by src will not be modified by the function. This let's me pass in a pointer to a const object:

const POD pod{...};
POD new_pod;
memcpy(&new_pod, &pod, sizeof(pod)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM