GCC和枚举的前向声明 [英] GCC and forward declaration of enum

查看:120
本文介绍了GCC和枚举的前向声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不使用GCC的新版本,使用下面的掷骰子

代码:


enum E;


enum E {e};


也就是说,它不接受enum的前向声明。 C ++标准文本

没有明确说明enum的前向声明,但是一个例子

在18.2.1中显示它,第4节:


enum float_round_style;

enum float_denorm_style;


这是标准中的缺陷吗?

Not quite new version of GCC that I have to use, craps with the following
code:

enum E;

enum E { e };

That is, it doesn''t accept forward declaration of enum. C++ standard text
doesn''t explicitly say about enum''s forward declaration, but one example
shows it in 18.2.1, clause 4:

enum float_round_style;
enum float_denorm_style;

Is it a defect in the standard?

推荐答案

Alexander Grigoriev写道:
Alexander Grigoriev wrote:
我不得不使用GCC的新版本,使用下面的代码来解读:

枚举E;

枚举E {e};

也就是说,它不接受枚举的前向声明。 C ++标准
文本没有明确说明enum的前向声明,但是一个
示例在18.2.1,第4节中显示:


枚举不能向前宣布。首先,C ++标准没有

定义前向声明。它定义了不完整的类型在3.9段中

6您可以阅读:


"已声明但未定义的类,或未知大小的数组

或不完整的元素类型,是一个未完全定义的对象类型。

不完全定义的对象类型和void类型是不完整的类型

(3.9.1 )。


(3.9.1仅指定义无效的部分)


所以看来这一段(及其非-normative example)定义了什么

可以是一个不完整的类型(可以向前声明的内容)。枚举复合物

类型不存在。


除此之外,第7.2节(描述枚举声明)并不是
提到声明不完整的枚举类型的任何可能性。因此我们

可以断定它是不允许的。


背景:在C ++中,枚举类型可以用_any_ integral表示

类型,不仅是int(见7.2 / 5)。它是实现定义的*

类型是什么,并且实现从枚举类型的完整定义

中得出结论。所以在以下情况下:


enum ColorsEnum;

....

struct foo {

ColorsEnum * p

};


编译器不知道那里的指针是否应指向int,

a (signed / unsigned)char,unsigned int或long int或......任何其他

整数类型。虽然在大多数架构上它可能不是问题,但在一些

体系结构中,指针将具有不同的大小,以防它是一个char

指针。所以最后我们想象中的编译器根本不知道要把

放到哪里来获得ColorsEnum *。


enum float_round_style;
enum float_denorm_style;

这是标准中的缺陷吗?
Not quite new version of GCC that I have to use, craps with the
following code:

enum E;

enum E { e };

That is, it doesn''t accept forward declaration of enum. C++ standard
text doesn''t explicitly say about enum''s forward declaration, but one
example shows it in 18.2.1, clause 4:
Enums cannot be forward declared. First of all the C++ standard does not
define forward declaration. It defines incomplete types. in 3.9 paragraph
6 you can read:

"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.
Incompletely-defined object types and the void types are incomplete types
(3.9.1)."

(3.9.1 only refers to the section defining void)

So it seems that this paragraph (and its non-normative example) defines what
can be an incomplete type (what can be forward declared). The enum compound
type is not there.

In addition to this, the section 7.2 (describing enum declarations) does not
mention any possibility to declare an incomplete enum type. Therefore we
can conclude that it is simply not allowed.

Background: in C++ enum types can be represented by using _any_ integral
type, not only int (see 7.2/5). It is implementation defined *what* that
type is, and the implementation figures it out from the complete definition
of the enum type. So in case of:

enum ColorsEnum;
....
struct foo {
ColorsEnum *p
};

The compiler would have no idea if the pointer there should point to an int,
a (signed/unsigned) char, an unsigned int or a long int or... any other
integral type. While on most architectures it may not be an issue, on some
architectures the pointer will have a different size, in case it is a char
pointer. So finally our imaginary compiler would have no idea what to put
there to get a ColorsEnum*.

enum float_round_style;
enum float_denorm_style;

Is it a defect in the standard?




示例不是标准文本的一部分。它们是非规范性的。


但你所展示的不是一个例子,它是一个概要。含义

" outline",design,summary,conspectus。它不是C ++代码,而是一些代码的部分代码,显示了受标准管理的东西。


根据公共WG21网站那里对此没有任何问题。

我建议你在comp.std.c ++中询问这个C ++的原因,但是

格式错误的表示法。 />

-

阿提拉又名WW



Examples are not part of the Standard text. They are non-normative.

But what you do show is not an example, it is a synopsis. Meaning
"outline", "design", "summary", "conspectus". It is not C++ code, but some
parts of the code showing those things which are governed by the standard.

According to the public WG21 web site there is no issue raised about this.
I suggest that you ask in comp.std.c++ for the reasons of this C++ like, but
ill-formed notation.

--
Attila aka WW


你的例子不太正确,因为你的论点也可以适用于
不完整的struct / class类型(编译器不知道

指针是否应该指向struct / class of what size?),并且在这种情况下它不会是b $ b。


实际问题是枚举通常按值传递,并且
$如果类型仍然不完整,b $ b编译器无法决定调用

时函数的枚举大小。


" ; Attila Feher <在********** @ lmf.ericsson.se>在消息中写道

news:bj ********** @ newstree.wise.edt.ericsson.se ...
Your example is not quite correct, as your argument can also be applied to
incomplete struct/class types ("The compiler would have no idea if the
pointer there should point to" struct/class of what size?), and it doesn''t
stand in that case.

The actual problem is that enums are usually passed by value, and the
compiler can''t decide what size of enum shuld be passed to the function when
called, if the type is still incomplete.

"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bj**********@newstree.wise.edt.ericsson.se...
背景:在C ++枚举中类型可以使用_any_ integral
类型来表示,而不仅仅是int(见7.2 / 5)。它是实现定义的*
类型是什么,并且实现从枚举类型的完整
定义中得出结论。所以在以下情况下:

枚举ColorsEnum;
...
struct foo {
ColorsEnum * p
};
编译器不知道那里的指针是否应该指向
int,一个(有符号/无符号)字符,一个unsigned int或一个long int或......任何其他
整数类型。虽然在大多数体系结构上它可能不是问题,但在
某些体系结构中,指针将具有不同的大小,以防它是一个char
指针。所以最后我们想象中的编译器根本不知道要把
放到哪里来获得ColorsEnum *。
Background: in C++ enum types can be represented by using _any_ integral
type, not only int (see 7.2/5). It is implementation defined *what* that
type is, and the implementation figures it out from the complete definition of the enum type. So in case of:

enum ColorsEnum;
...
struct foo {
ColorsEnum *p
};

The compiler would have no idea if the pointer there should point to an int, a (signed/unsigned) char, an unsigned int or a long int or... any other
integral type. While on most architectures it may not be an issue, on some architectures the pointer will have a different size, in case it is a char
pointer. So finally our imaginary compiler would have no idea what to put
there to get a ColorsEnum*.



Alexander Grigoriev写道:
Alexander Grigoriev wrote:
你的例子不太正确,因为你的论证也可以应用于不完整的struct / class类型(如果那里的指针应指向struct,那么编译器就没有想法。 /什么尺寸?),并且在这种情况下它并不存在。


请不要发帖。谢谢。


有一个非常非常重要的区别。对于类来说,

有意义转发声明它们,因为它们可以相互引用,其中

的情况下代码无法写入。枚举不能互相引用

方式。因此在类类型中需要统一正在使用的指针。

由于枚举声明无法获得递归,因此没有任何意义,所以

为什么会你有限制吗?

实际问题是枚举通常是按值传递的,而
编译器无法决定将枚举的大小传递给
函数调用时,如果类型仍然不完整。
Your example is not quite correct, as your argument can also be
applied to incomplete struct/class types ("The compiler would have no
idea if the pointer there should point to" struct/class of what
size?), and it doesn''t stand in that case.
PLEASE do NOT top post. Thank you.

There is a very very very very important difference. For classes it makes
sense to forward declare them, since they can refer to each other, in which
case the code could not be written. Enums cannot refer to each other that
way. So in class types there is a need for unifying the pointer being used.
Since enum declarations cannot get "recursive", there it makes no sense, so
why would you make restrictions?
The actual problem is that enums are usually passed by value, and the
compiler can''t decide what size of enum shuld be passed to the
function when called, if the type is still incomplete.




不完整类型*不能*按值传递。对不起,但是你在这里和B / S说话。

那个B / S不是* Bjarne Stroustrup,但是与消化过程的

结束有关在大型动物中,西班牙人喜欢在竞技场中杀人。


-

WW又名阿提拉



Incomplete types *cannot* be passed by value. Sorry, but you talk B/S here.
And that B/S is *not* Bjarne Stroustrup, but has something to do with the
end of the digestion process in big animals Spanish like to kill in arenas.

--
WW aka Attila


这篇关于GCC和枚举的前向声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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