为什么 C 语言中不能创建 void 类型的对象?
C++ 不允许创建 void
类型的对象。这是为什么?
C++ doesn't allow creating objects of type void
. Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
C++ 不允许创建 void
类型的对象。这是为什么?
C++ doesn't allow creating objects of type void
. Why is that?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
考虑下面的代码。
为了知道如何实际创建对象,编译器必须知道类型。非正式地,您可以将
void
视为表示类型缺失的“类型”。因此,编译器不可能知道如何创建void
对象。你无法创建一个你不知道如何创建的对象。话虽如此,在其他情况下,
void
也是有意义的。例如,void
函数没有返回值。您不能为不存在的事物(例如返回值)分配类型。您还可以有一个指向
void
或void*
的指针。与普通的void
不同,void*
是有效的,仅表示“指向某种未知类型的对象的指针”。显然,除非将void*
转换为实际的已知类型(当然假设转换是安全的),否则您无法对它做太多事情。Consider the code below.
In order to know how to actually create the object, the compiler has to know the type. Informally, you can think of
void
as a "type" representing an absence of type. Therefore, the compiler can't possibly know how to create avoid
object. You can't create an object you don't know how to create.That being said, there are other cases where
void
makes sense. For example, avoid
function has no return value. You can't assign a type to things (like return values) that do not exist.You can also have a pointer to
void
, orvoid*
. Unlike the plainvoid
,void*
is valid and simply means "a pointer to an object of some unknown type". Obviously you can't do much with avoid*
until you cast it into an actual, known type (assuming the cast is safe, of course).这是因为
void
是一个不完整的类型。从标准文档来看,不完整类型 3.9 指出,
由于
void
是不完整类型,因此无法确定其大小和布局,因此它无法定义。It is because
void
is an incomplete type.From Standard docs., incomplete types 3.9 states that,
Since
void
is an incomplete type, it's size and layout cannot be determined and hence it cannot be defined.附带说明一下,您可以创建一个
void
类型的临时文件:这是有效的代码。它在通用代码中非常有用。它甚至被认为允许在某些地方(例如基类)使用内置类型(包括 void):
As a side note, you can create a temporary of type
void
:this is valid code. It is extremely useful in generic code. It was even considered to allow the usage of built in types (including void) in some places like base classes:
void
是一个占位符,表示不需要任何类型的对象。作为函数参数规范
历史上,C 在函数声明中使用空参数列表 ala
return_type f();
来允许以任意多的方式调用f()
-type 参数在每个调用站点指定,而return_type f(void);
明确表明不需要或允许任何参数。即,C 准备相信程序员能够获得正确的参数数量和类型,任何错误都可能损坏数据和/或使程序崩溃。作为函数返回类型
如果没有
void
来建立作为语言语法一部分的整体“类型变量|函数”序列,那么语言中也会存在一些歧义。例如:与其他类型的比较
从表示内存区域的某种解释的意义上来说,它本身并不是真正的数据类型,如 int、float、char 等、类、联合等。
指向 void 的指针
对于
void*
,它表示无法洞察所包含地址处的内存内容,因此在取消引用指针之前,必须将其强制转换为反映按位布局的特定类型该内存地址处的数据。然后,编译器可以根据当时已知的数据类型对该位布局进行解释和操作。void
is a placeholder indicating no object of any type is expected.As a function argument specification
Historically C used an empty argument list in a function declaration ala
return_type f();
to allowf()
to be called with however-many and whatever-type arguments were specified at each call site, whereasreturn_type f(void);
made it explicit no arguments were expected or allowed. I.e. C was prepared to trust the programmer to get the number and types of arguments right, with any mistake likely to corrupt data and/or crash the program.As a function return type
There woud also be some ambiguities in the language if
void
wasn't there to establish the overall "type variable|function" sequence that's part of the grammar of the language. For example:Comparison with other types
It is not really a data type itself in the sense of representing some interpretation of an area of memory, as int, float, char etc., classes, unions etc. all do.
pointers to void
For
void*
, it indicates a loss of insight into the contents of memory at the contained address, such that sometime before dereferencing the pointer it must be cast to a specific type reflecting the bitwise layout of data at that memory address. Then, the compiler can interpret and operate on that bit layout in accord with the then-known data type.在
C++
中,任何事物都可以与对象相关。因此,当说 -平台必须在不知道变量类型的情况下为变量分配多少字节。是
int
或float
或double
还是任何其他原始类型。所以,这是不允许的。In
C++
, every thing can be related to object. So, when said -How many bytes must the platform allocate for
variable
with out knowing it's type. Is itint
orfloat
ordouble
or any other primitive type. So, it is not allowed.void 代表 Nothing。即使您使用 Void 类型创建函数,我们也不需要返回值。同样,这里也没有声明 void 类型的对象。
void represents Nothing.Even you are creating function with Void type we no need to return a value.Same like Here also No object of type void is declared.
简单地说,它是 C 中的任意决定。
在 C 中,所有类型(除了
void
之外)都用于携带值。void
,根据定义,不具有任何值。因此,语言设计者决定不可能实例化它。C++ 追随 C,但决定仍然存在。否则就需要定义一个存储大小(可能与
bool
相同)。在 C++ 中,这确实很烦人,特别是因为模板类/函数需要特殊的大小写,但没有人认为它值得修改,因为可以专门化
void
模板,因此它不会阻塞。It is, simply, an arbitrary decision, from C.
In C, all types (except from
void
) are used to carry a value.void
, by definition, does not hold any value. The language designers therefore decided it would not be possible to instantiate it.C++ takes after C, and the decision remained. Otherwise it would have been necessary to define a storage size (probably the same as
bool
).In C++ it is indeed annoying, especially because of the special casing necessary for template classes / functions, but no-one deemed it worthy of modification since it's possible to specialize the templates for
void
and thus it's not blocking.