当阵列涉及不应声明符合其定义是什么? [英] Shouldn't declaration match its definition when array is involved?

查看:145
本文介绍了当阵列涉及不应声明符合其定义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个源文件在我的计划。

There are two source files in my program.

这是数组中定义的 A.cpp

// compiler: MSVC2005 SP2    
// A.cpp

// defines an array of type "int [100]"
int a[100] = {3};

据中使用的 B.cpp

// B.cpp

// declares an array of type "int []"
extern int a[];

int main()
{
  // prints 3 correctly
  cout << a[0] << endl;
  return 0;
}

据我所知,连接器将引发一个错误,如果它不能找到一个声明,任何匹配的定义,如果使用了声明标识符。在这里, INT [] INT [100] 有两种不同的类型,很明显。

AFAIK, linker will raise an error if it cannot find any matched definition for a declaration if the declared identifier is used. Here, int [] and int [100] are two different types, obviously.

为什么在这种情况下,是不是有什么环节出错?是否通过担保的标准的数组大小声明/定义的匹配过程中琐碎的?或者,它只是实现特定的?从引述标准的将是如有pciated AP $ P $。

Why, in this case, isn't there any link error? Is it guaranteed by the Standard that array size is trivial during matching of declaration/definition? Or it's just implementation-specific? A quote from the Standard will be appreciated if any.

编辑:在他的答案iammilind提到,链接器可以正确运行(编译他是gcc)的即使类型不声明和定义之间的匹配。它是由标准或要求的只是一个gcc的方式吗?我想这是要弄清楚一个更为重要的问题。

iammilind mentioned in his answer that linker can run correctly(his compiler is gcc) even if the type does NOT match between declaration and definition. Is it REQUIRED by the Standard or just a way of gcc? I guess this is a far more important issue to figure out.

推荐答案

在C和C ++对象的声明 A 不完全型的匹配对象的定义 A 其中,型号齐全。你只需观察说明,在C ++中你被允许在非定义声明来使用不完全类型的事实。但是一旦你定义的类型必须是完整的。

In both C and C++ a declaration of an object a of incomplete type will match the definition of object a where the type is complete. What you observe simply illustrates the fact that in C++ you are allowed to use incomplete types in non-defining declarations. But once you get to the definition the type must be complete.

此行​​为并不限于阵列。例如,你可以声明

This behavior is not restricted to arrays. For example, you can declare

extern class X x;

对于一个完全未知的类 X ,然后,当类X 已经完全定义,你可以定义

for a totally unknown class X, and then, when class X is already fully defined, you can define

X x;

这将匹配上述声明。

which will match to the above declaration.

同样的事情发生与您的阵列。首先声明不完全类型的对象

The same thing happens with your array. Firstly you declare an object of incomplete type

extern int a[];

,然后你用完整的类型定义它。

and then you define it with complete type

int a[100];

在这里确实类型不匹配。但是,C ++语言从来没有要求他们相匹配。例如,3.9 / 7明确规定

The types here indeed are not matching. However, the C++ language never required them to match. For example, 3.9/7 explicitly states

一个数组对象声明的类型可能是未知大小的数组
  因此是不完整的在一个点在翻译单元和
  完成以后;在这两个点的数组类型(的数组
  未知约束的T和N的T阵)是不同的类型。

The declared type of an array object might be an array of unknown size and therefore be incomplete at one point in a translation unit and complete later on; the array types at those two points ("array of unknown bound of T" and "array of N T") are different types.

这意味着的相同的数组对象可以初步具有不完整的类型,但后来获得一个完整的类型。 (另见3.9 / 7的例子)。这不,当然意味着你可以声明 A INT ,然后将其定义为双击。你在这里的唯一类型相关的自由,再次完成不完全类型。没有更多的。

It means that the same array object can have incomplete type initially, but acquire a complete type later on. (See also the example in 3.9/7). This doesn't, of course, mean that you can declare a as an int and then define it as a double. The only type-related freedom you have here is, again, to complete an incomplete type. No more.

这篇关于当阵列涉及不应声明符合其定义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆