结构上的==运算符 [英] == operator on struct

查看:95
本文介绍了结构上的==运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自印度的问候


我想知道允许结构分配的理由

(使用=运算符)并且不允许使用相等运算符(

他们。


编译器在使用=时识别漏洞等同样的

然后编译器应该能够生成代码来比较单个

struct字段。


当一个新字段被添加到struct时,所有这样的实例都在

==使用个别字段进行比较需要修改并且是一个真实的痛苦。 :-(


提前致谢,

- Sushil Ramaswamy

Greetings from India

I would like to know the rationale on allowing structs to be assigned
(using = operator) and not allowing equality operator ( == operator) on
them.

The compiler when it assigns using = is aware of holes etc and the same
compiler then should be able to generate code to compare the individual
struct fields.

When a new field is added to the struct, all such instances where the
== using individual field is compared need to be modified and is a real
pain. :-(

Thanks in advance,
- Sushil Ramaswamy

推荐答案



in*******@yahoo.com 写道:
来自印度的问候

我想知道允许结构分配的基本原理
(使用=运算符)并且不允许使用相等运算符(=
他们。
Greetings from India

I would like to know the rationale on allowing structs to be assigned
(using = operator) and not allowing equality operator ( == operator) on
them.




如果你想知道基本原理,谷歌的


..


..


..


..


..


..


(等待它......)


..


..


..


..


..


..


" C Rationale" !!!!


(遗憾的是,基本原理并没有解释为什么

委员会给出了这样一个反直觉的名字

理由。我的意思是,在他的正确思想中,谁会使用基本原理这个词。当试图找到理由时?

这是不合理的,那是什么。)


-
呃********* @ sun.com



If you want to know the rationale, Google for

..

..

..

..

..

..

(wait for it ...)

..

..

..

..

..

..

"C Rationale"!!!!

(Unfortunately, the Rationale doesn''t explain why the
committee gave such a counter-intuitive name to the
Rationale. I mean, who in his right mind would use
the word "Rationale" when trying to find a rationale?
It''s irrational, that''s what.)

--
Er*********@sun.com

< br>

文章< 11 ********************* @ g44g2000cwa.googlegroups。 com>,

< in ******* @ yahoo.com>写道:
In article <11*********************@g44g2000cwa.googlegroups. com>,
<in*******@yahoo.com> wrote:

我想知道允许结构分配的基本原理
(使用=运算符)并且不允许使用等于运算符(==运算符)它们。

编译器在使用=时会识别漏洞等,然后相同的编译器应该能够生成代码来比较各个结构域。<当一个新字段被添加到结构中时,所有这些比较
==使用单个字段的实例都需要修改并且是一个真正的痛苦。 : - (

I would like to know the rationale on allowing structs to be assigned
(using = operator) and not allowing equality operator ( == operator) on
them.

The compiler when it assigns using = is aware of holes etc and the same
compiler then should be able to generate code to compare the individual
struct fields.

When a new field is added to the struct, all such instances where the
== using individual field is compared need to be modified and is a real
pain. :-(




这样的功能并不像它看起来那么有用。


- 包含浮点的结构数字无法保证

比较平等,除非是琐碎的情况。


- 结构越大,成员越有可能

用于比较目的是无趣的。


- 如果有结构相等运算符,那么最终会有一些

gmail或yahoo用户会过来问为什么有没有结构不等式运算符,如果存在这些运算符,为什么

不能传递给bsearch()和qsort() ,如果他们

为什么他们不能参数化给予不同的重量和订购给不同的结构成员......


- 一个复杂的等式运算符违背了基本的C原则

表示语言映射的基本操作非常简短
(和快速)cpu序列结构。

-

7842 ++



Such a feature isn''t as useful as it might seem.

- structs containing floating point numbers cannot be guaranteed
to ever compare as equal, except in trivial cases.

- the larger the struct, the more likely there are members which
are uninteresting for comparison purposes.

- if there was a struct-equality operator, then eventually some
gmail or yahoo user would come along and ask why there are
no struct-inequality operators, and if those existed, why
can''t they be passed to bsearch() and qsort(), and if they
could why can''t they be parameterized to give different
weight and ordering to different struct members...

- a complicated equality operator works against the basic C principle
that the basic operation of the language map to very short
(and fast) sequences of cpu instructions.
--
7842++


在******* @ yahoo.com 中写道:

来自印度的问候

我想知道允许结构分配
(使用=运算符)并且不允许使用等于运算符(==运算符)的原理。
来自C-Rationale:

Greetings from India

I would like to know the rationale on allowing structs to be assigned
(using = operator) and not allowing equality operator ( == operator) on
them. From the C-Rationale:




C89委员会不止一次考虑过,

允许比较平等的结构。这些建议

因结构上的漏洞问题而失败。两个结构的字节方式比较b / b
肯定会将孔确定为零,这样所有的孔都会相等,

a自动或动态分配

变量的艰巨任务。结构中联合类型元素的可能性

引发了这种方法不可克服的问题。如果没有

保证所有的孔都设置为零,那么实现

就必须准备好将结构比较分解为

一个任意的成员比较的数量;一个看似简单的

表达式因此可以扩展为大量的代码,

这违背了C的精神。


-

Chuck F(cb********@yahoo.com)(cb ******** @ worldnet.att.net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>使用worldnet地址!



"The C89 Committee considered, on more than one occasion,
permitting comparison of structures for equality. Such proposals
foundered on the problem of holes in structures. A byte-wise
comparison of two structures would require that the holes
assuredly be set to zero so that all holes would compare equal,
a difficult task for automatic or dynamically allocated
variables. The possibility of union-type elements in a structure
raises insuperable problems with this approach. Without the
assurance that all holes were set to zero, the implementation
would have to be prepared to break a structure comparison into
an arbitrary number of member comparisons; a seemingly simple
expression could thus expand into a substantial stretch of code,
which is contrary to the spirit of C."

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


这篇关于结构上的==运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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