finding composed structs

Timon Gehr timon.gehr at gmx.ch
Tue Oct 30 15:55:30 PDT 2012


On 10/30/2012 09:16 PM, Tobias Pankrath wrote:
> On Tuesday, 30 October 2012 at 19:16:18 UTC, Dan wrote:
>> So until this bug is fixed any time I have any dynamic array,
>> including string in struct S, implement opEquals. When the bug is
>> fixed I can remove them. What I didn't realize is that including an
>> opEquals in A will cause generation of opEquals in B,C,D for free
>> (maybe only if called).
>>
>> If this is still off base please let me know.
>>
>> Thanks
>> Dan
>
> In the meantime you can use this function template to compare structs
> field by field.
>
> bool oneStepEqual(T,F)(ref T lhs, ref F rhs)
>      if(is(Unqual!T == Unqual!F) && is(T == struct))
> {
>          bool result = true;
>      foreach(mem; __traits(allMembers, T))
>      {
>          static if(!is(typeof(__traits(getMember, T, mem)) == function))
>          {
>              result = result && (__traits(getMember, lhs, mem) ==
> __traits(getMember, rhs, mem));
>          }
>      }
>      return result;
> }
>

You might want to shortcut after the first failed comparison.


More information about the Digitalmars-d-learn mailing list