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 :

Upvotes: 9

Views: 1111

Answers (4)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385274

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

Upvotes: 7

Barry
Barry

Reputation: 303357

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

Upvotes: 3

Deduplicator
Deduplicator

Reputation: 45674

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

Upvotes: 3

user1084944
user1084944

Reputation:

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.

Upvotes: 4

Related Questions