C++'s "defaulted comparison operators" proposal

Artur Skawina via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jun 18 04:48:32 PDT 2014


On 06/18/14 07:49, Ali Çehreli via Digitalmars-d-learn wrote:

> The idea is to be able to have standard comparison behavior for structs without boilerplate. For example, if the ordering should consider all members of the following struct one after the other (first 'i', then 'l', and finally 's'):

Isn't this already implicitly done? (I thought it was just my old compiler
version that did not yet support this)

Anyway, something like this wrapped in an appropriate template
should do:

   int opCmp()(const typeof(this) b) const {
      foreach (I, _; typeof(this.tupleof))
         static if (is(typeof(this.tupleof[I].opCmp(b.tupleof[I])))) {
            if (auto d = this.tupleof[I].opCmp(b.tupleof[I]))
                return d;
         } else {
            if (auto d = this.tupleof[I] - b.tupleof[I])
                return d;
         }
      return 0;
   }

and

http://forum.dlang.org/post/mailman.1230.1333044904.4860.digitalmars-d@puremagic.com

artur


More information about the Digitalmars-d-learn mailing list