default '==' on structs

bearophile bearophileHUGS at lycos.com
Wed Feb 2 11:20:31 PST 2011


spir:

> Do you know more about why/how the above fails?

It's simple. A string (or array) is a 2-words long struct that contains a pointer to the data and a size_t length. Default struct equality just compares the bits of those two fields. In the above example I have created f1 and f2 using two strings that have the same contents and lengths, but the pointers are different, because they are generated at run-time (normally the compiler uses a pool of shared string literals), so the equality fails.

I have asked Walter to fix this problem with strings and arrays probably three years ago or more, it's not a new problem :-)

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list