Tomás Ó hÉilidhe wrote:
> On Dec 18, 6:43*am, CBFalconer <cbfalco...@yahoo.com> wrote:
>
>> Yes, provided s1 and s2 are the same struct type. *What you cannot
>> do (because of padding, etc) is compare them for equality. *i.e.:
>>
>> if (s1 == s2) ...
>>
>> is not legal.
>
> I'm very surprised at this. I tried it just there with a C compiler
> and also with a C++ compiler, and both gave a compiler error. I don't
> see why they couldn't have made the comparison result in a comparison
> of all members, something like:
>
> #define (a == b) (a.i == b.i && a.j == b.j && a.k == b.k)
Because that's often the wrong answer. Consider (a) structs with
`char*` members: what's the (one, single, only) right way to compare them?
Consider (b) this (illustrative) example:
struct SmallStringBuffer { char[17] content; int size; }
I have in my left hand a StringBuffer with content [`A`, 0 (16)]
(the parenthesised number is a repeat count), and size 1, and in my
right hand one with size 1 and content [`A`, `B` (16)]. Are they
equal?
If the compiler/language can't guess the right answer often enough,
perhaps it doesn't need to guess at all.
> So if you want to compare two structs for equality (I'm talking value
> equality as opposed to binary equality), then you actually have to
> verbosely compare member by member? That's a pain.
Slightly. But only slightly: that's why the gods gave us functions.
Of course, it would be nice if it was easy to use the functions without
lots of syntactic clutter. But lets not overload this response with that
discussion.
--
"We dance, and the worlds melt away." - Curved Air, /Metamorphosis/
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
|