什么是const void? [英] What is const void?
问题描述
如果T是类型,则提供等于true的成员常量值。 void,const void,volatile
void,或const volatile void。
Provides the member constant value that is equal to true, if T is the type void, const void, volatile void, or const volatile void.
然后可能是 const void
或 volatile void
吗?
此答案指出 const void
返回类型将无效(但是在VC ++ 2015上编译)
This answer states that const void
return type would be invalid (however compiles on VC++ 2015)
const void foo() { }
如果按照标准, const void
为无效(VC错误)-那么 const void
是什么?
If by standard, const void
is invalid (VC being wrong) - then what is const void
?
推荐答案
const void
是一种可以形成指向的类型。它类似于普通的void指针,但转换的工作方式不同。例如, const int *
不能隐式转换为 void *
,但可以隐式转换为 const void *
。同样,如果您有 const void *
,则不能将其 static_cast
转换为 int *
,但您可以将它 static_cast
转换为 const int *
。
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*
. Likewise, if you have a const void*
you cannot static_cast
it to an int*
, but you can static_cast
it to a 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
这篇关于什么是const void?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!