gpt4 book ai didi

c++ - `void` "objects": which part of C++ standard prohibits them? 的非定义声明或者是吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:18:13 25 4
gpt4 key购买 nike

C++ 标准中至少有两个地方禁止定义具有不完整类型的对象(http://eel.is/c++draft/basic.def#5http://eel.is/c++draft/basic.types#5)。但是,在 C++ 中通常允许为不完整类型的对象提供非定义声明。而且我似乎无法查明禁止以这种方式声明 void 类型的不完整“对象”的特定部分。 (当然,void 不是 C++ 中的对象类型,但举个例子,引用类型也不是。)所以,这是

extern void a;

在 C++ 中真的是病式的吗?

在 C 中,允许为 void 对象(如上所示)提供非定义声明,并且 GCC 和 Clang 都接受 C 代码中的上述内容(当然,不允许定义)。但在 C++ 代码中,两个编译器都会针对此类声明发出错误。标准的哪一部分要求他们这样做?

[basic.fundamental] 列出了 void 类型 ( http://eel.is/c++draft/basic.types#basic.fundamental-13 ) 的可能用途,但它似乎并不是一个完整列表。

最佳答案

我认为相关段落如下:

[dcl.stc]

5 The extern specifier shall be applied only to the declaration of a variable or function.

[basic]

6 A variable is introduced by the declaration of a reference other than a non-static data member or of an object. The variable's name, if any, denotes the reference or object.

[basic.types]

8 An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not cv void.

a,作为变量声明,必须根据 [basic]¶6 表示引用或对象。这涵盖了确实不是对象类型的引用。但是,由于 void 既不是引用也不是对象类型,因此声明格式错误。

关于c++ - `void` "objects": which part of C++ standard prohibits them? 的非定义声明或者是吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53976019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com