gpt4 book ai didi

来自 8.3.4p4 的 C++11 数组声明示例与 gcc 或 clang 不匹配?

转载 作者:行者123 更新时间:2023-11-30 01:56:08 25 4
gpt4 key购买 nike

这是 C++11 示例的子集(与最新草案相同)8.3.4p4 ([dcl.array]/4):

extern int x[10];                   // 1

int x[]; // OK: bound is 10 // 2

void f() {
extern int x[]; // 3
int i = sizeof(x); // error incomplete type
}

gcc 不同意错误并给出:

error: type mismatch with previous external decl of ‘int x []’

取而代之(在声明 3 处)。

clang 编译没有错误。

我的解释是该示例格式正确。声明 3 应链接到声明 1 和 2,因此应具有类型 array of 10 int,而不是 array of unknown bound int。所以在sizeof应该是完整的。

谁是对的?标准,gcc 还是 clang?

最佳答案

8.3.4/3 包括:

Furthermore, if there is a preceding declaration of the entity in the same scope in which the bound was specified, an omitted array bound is taken to be the same as in that earlier declaration,

由于函数内部的声明与不适用的全局声明不在同一范围内,因此不授予省略数组绑定(bind)的权限。我也没有看到任何其他允许省略的文本,因此 sizeof(x) 表达式中的 x 类型是不完整的。

Clang 不应该接受代码,gcc 以错误的理由拒绝它,标准的示例对我来说看起来很准确。

关于来自 8.3.4p4 的 C++11 数组声明示例与 gcc 或 clang 不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20129495/

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