[英]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 ) : 我在最新的C ++标准草案( N4606 )中遇到过类型“cv void” :
8.3.3 [dcl.mptr], paragraph 3
8.3.3 [dcl.mptr],第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” .
指向成员的指针不应指向类(9.2.3)的静态成员,具有引用类型的成员或“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 . 通过一些研究,我发现“cv void”是一个真实的类型 ,但我不知道与void类型相比有什么区别。 Can you explain it with an example (maybe with a code) ?
你能用一个例子 (可能带代码)解释一下吗?
EDIT : 编辑 :
3.9.1 [basic.fundamental], paragraph 9
3.9.1 [basic.fundamental],第9段
A type cv void is an incomplete type that cannot be completed;
类型cv void是不完整的类型,无法完成; such a type has an empty set of values...
这样的类型有一组空值...
"cv void" is not a real type. “cv void”不是真正的类型。 "cv" here is a shorthand for "possibly cv-qualified", which means "may have a
const
or a volatile
on it". “cv”这里是“可能是cv-qualified”的简写,这意味着“可能有一个
const
或一个volatile
”。
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
. 该段落意味着指向成员的指针可能不指向以下类型的对象:
void
, const void
, volatile void
和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. 我们并不“需要”允许
void
被cv限定。 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
. 但它实际上确实有一个重要的实际用途:允许
void
cv-qualified允许我们用指针到void
来编写cv-correct代码。
"cv void
" means void
which is optionally const
- or volatile
-qualified. “cv
void
”表示可选地为const
或volatile
标准化的void
。 Simply void
is not so qualified. 简单
void
并不合格。
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 typevoid
?为什么我们需要“
cv
-qualify”类型void
?
Same reason you would need to cv -qualify any other type. 同样的原因你需要cv -qualify任何其他类型。 For instance,
memcpy
's signature is: 例如,
memcpy
的签名是:
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. 该
src
参数是一个指向const void
,这是因为存储器所指向src
不会被功能进行修改。 This let's me pass in a pointer to a const
object: 这让我传入一个指向
const
对象的指针:
const POD pod{...};
POD new_pod;
memcpy(&new_pod, &pod, sizeof(pod));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.