为什么 C 语言中不能创建 void 类型的对象?

发布于 2024-10-26 16:20:55 字数 47 浏览 6 评论 0原文

C++ 不允许创建 void 类型的对象。这是为什么?

C++ doesn't allow creating objects of type void. Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

陌路黄昏 2024-11-02 16:20:55

考虑下面的代码。

class Foo
{
    // etc ...
};

int main()
{
    // Declaring an object...
    Foo foo;
    // foo has been created.

    // etc ...

    return 0; // When function returns, foo will be destroyed.
}

为了知道如何实际创建对象,编译器必须知道类型。非正式地,您可以将 void 视为表示类型缺失的“类型”。因此,编译器不可能知道如何创建 void 对象。你无法创建一个你不知道如何创建的对象。

int main()
{
    void v; // "I don't know how to create this!"
}

话虽如此,在其他情况下,void 也是有意义的。例如,void 函数没有返回值。您不能为不存在的事物(例如返回值)分配类型。

您还可以有一个指向 voidvoid* 的指针。与普通的 void 不同,void* 是有效的,仅表示“指向某种未知类型的对象的指针”。显然,除非将 void* 转换为实际的已知类型(当然假设转换是安全的),否则您无法对它做太多事情。

Consider the code below.

class Foo
{
    // etc ...
};

int main()
{
    // Declaring an object...
    Foo foo;
    // foo has been created.

    // etc ...

    return 0; // When function returns, foo will be destroyed.
}

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 a void object. You can't create an object you don't know how to create.

int main()
{
    void v; // "I don't know how to create this!"
}

That being said, there are other cases where void makes sense. For example, a void 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, or void*. Unlike the plain void, void* is valid and simply means "a pointer to an object of some unknown type". Obviously you can't do much with a void* until you cast it into an actual, known type (assuming the cast is safe, of course).

凡间太子 2024-11-02 16:20:55

这是因为 void 是一个不完整的类型。

从标准文档来看,不完整类型 3.9 指出,

5 已声明但未定义的类,或者未知大小或不完整元素类型的数组,是一个
不完全定义的对象类型。38) 不完全定义的对象类型和void 类型是不完全类型 (3.9.1)。
对象不应被定义为具有不完整的类型。

38) 未完全定义的对象类型的实例的大小和布局未知。

由于 void 是不完整类型,因此无法确定其大小和布局,因此它无法定义。

It is because void is an incomplete type.

From Standard docs., incomplete types 3.9 states that,

5 A class that has been declared but not defined, or an array of unknown size or of incomplete element type, is an
incompletely-defined object type.38) Incompletely-defined object types and the void types are incomplete types (3.9.1).
Objects shall not be defined to have an incomplete type.

38) The size and layout of an instance of an incompletely-defined object type is unknown.

Since void is an incomplete type, it's size and layout cannot be determined and hence it cannot be defined.

一张白纸 2024-11-02 16:20:55

附带说明一下,您可以创建一个 void 类型的临时文件:

void f()
{
    return void();
}

void g()
{
    return cout << "hi", f();
}

这是有效的代码。它在通用代码中非常有用。它甚至被认为允许在某些地方(例如基类)使用内置类型(包括 void):

template<class T> class A : T { };

A<string> x; // works
A<int> y; // error, but could be usefull
A<void> z; // error, but could be usefull.

As a side note, you can create a temporary of type void:

void f()
{
    return void();
}

void g()
{
    return cout << "hi", f();
}

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:

template<class T> class A : T { };

A<string> x; // works
A<int> y; // error, but could be usefull
A<void> z; // error, but could be usefull.
水水月牙 2024-11-02 16:20:55

void 是一个占位符,表示不需要任何类型的对象

作为函数参数规范

历史上,C 在函数声明中使用空参数列表 ala return_type f(); 来允许以任意多的方式调用 f() -type 参数在每个调用站点指定,而 return_type f(void); 明确表明不需要或允许任何参数。即,C 准备相信程序员能够获得正确的参数数量和类型,任何错误都可能损坏数据和/或使程序崩溃。

作为函数返回类型

如果没有 void 来建立作为语言语法一部分的整体“类型变量|函数”序列,那么语言中也会存在一些歧义。例如:

f();  // declaration of a function that returns nothing?
      // OR a function call?

与其他类型的比较

从表示内存区域的某种解释的意义上来说,它本身并不是真正的数据类型,如 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 allow f() to be called with however-many and whatever-type arguments were specified at each call site, whereas return_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:

f();  // declaration of a function that returns nothing?
      // OR a function call?

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.

独享拥抱 2024-11-02 16:20:55

C++中,任何事物都可以与对象相关。因此,当说 -

void variable ;  

平台必须在不知道变量类型的情况下为变量分配多少字节。是 intfloatdouble 还是任何其他原始类型。所以,这是不允许的。

In C++, every thing can be related to object. So, when said -

void variable ;  

How many bytes must the platform allocate for variable with out knowing it's type. Is it int or float or double or any other primitive type. So, it is not allowed.

撩发小公举 2024-11-02 16:20:55

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.

无人接听 2024-11-02 16:20:55

简单地说,它是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文