Why D is annoying =P

Rob T rob at ucora.com
Tue Oct 30 00:14:59 PDT 2012


On Tuesday, 30 October 2012 at 02:27:30 UTC, Mehrdad wrote:
>
> I understand the problem, but it doesn't seem related to 
> structs at all.
>
> Any two attempts to compare two default-valued floats will 
> fail, irrespective of whether or not they're inside structs.

True at the level of float by float comparisons, but not 
necessarilly true when struct by struct bit pattern compares are 
done.

My original understandiong of the struct type in D, is that it is 
defined as a POD which means == (without override) is done as a 
simple bit pattern compare, rather than doing a value by value 
compare of member data types. If this is the case, then two 
structs with default valued equivalent float types should compare 
equal, even though a value by value compare would compare not 
equal.

> TDPL clearly states that each of the struct's members are 
> supposed to
> be checked for equality (see section 7.1.5.2, p. 258 - 259).

So I guess that the POD definition of struct was changed at some 
point?

If so, then how are value by value compares performed when unions 
are involved? What about when there are member pointers, and 
other structures that cannot be compared in a meaningful way by 
default?

I guess the question is if the bit pattern comparision of structs 
has sufficient justification to be worth having as the default 
behaviour, or if there is more value attempting a value by value 
comparision, even though in many situations it may be pointless 
to attempt either strategy.

In my experience, I cannot recall doing very many direct struct 
comparisons either as a complete value by value compare or as a 
full bit pattern compare, I just never found it to be all that 
useful, no matter, I can try and imagine both strategies being 
useful in some situations.

With operator overloading you can have both strategies, but only 
if bit pattern compare is the default stategy, plus with 
overloading, compare can do exactly what you want to make sense 
out of unions and pointers, etc. So we should be good as is. I 
would expect that most times no one compares structs fully by 
value or by bit pattern anyway, to do so very often is difficult 
for me to imagine, but I could be wrong.

--rt



More information about the Digitalmars-d mailing list