简体   繁体   English

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

[英]C++11 array declaration example from 8.3.4p4 doesn't match gcc or clang?

This is a subset of an example from C++11 (same through to latest draft) 8.3.4p4 ([dcl.array]/4): 这是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 does not agree with the error and gives: gcc不同意错误并给出:

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

instead (at declaration 3). 相反(在声明3)。

clang compiles without error. clang编译没有错误。

My intepretation would be that the example is well-formed. 我的解释是,这个例子是格式良好的。 Declaration 3 should link to declaration 1 and 2, and should therefore have type array of 10 int , and not array of unknown bound int . 声明3应链接到声明1和2,因此应该具有array of 10 int类型array of 10 int ,而不是array of unknown bound int So it should be complete at the sizeof. 所以应该在sizeof完成。

Who is correct? 谁是对的? The standard, gcc or clang? 标准,gcc还是clang?

8.3.4/3 includes: 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, 此外,如果在指定边界的同一范围内存在实体的先前声明,则省略的数组绑定与先前声明中的相同

Since the declaration inside the function is not in the same scope as the global declaration that doesn't apply, and therefore does not grant permission to omit the array bound. 由于函数内部的声明与不适用的全局声明的范围不同,因此不授予省略数组绑定的权限。 I don't see any other text that would permit the omission either, and therefore the type of x in the sizeof(x) expression is incomplete. 我没有看到任何其他允许遗漏的文本,因此sizeof(x)表达式中x的类型是不完整的。

Clang should not be accepting the code, gcc is rejecting it for the wrong reason, and the standard's example looks accurate to me. Clang不应该接受代码,gcc因为错误的原因而拒绝它,标准的例子看起来很准确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM