Thread overview
C++'s "defaulted comparison operators" proposal
Jun 18, 2014
Ali Çehreli
Jun 18, 2014
Artur Skawina
Jun 18, 2014
Ellery Newcomer
June 18, 2014
Can you come up with a D library solution to the following C++11 proposal:

  http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2014/n3950.html

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'):

struct S
{
    int i;
    long l;
    string s;

    mixin DefaultComparisons!();    // something like this?
}

Or perhaps with an explicit comparison order? For example, to use 'l' as the primary key instead, and 'i' as the secondary key, and to ignore 'buffer' altogether:

struct S
{
    @Order(2)    int i;
    @Order(1)    long l;
    @OrderIgnore string buffer;

    mixin DefaultComparisons!();    // ?
}

Thank you,
Ali
June 18, 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
June 18, 2014
On Wednesday, 18 June 2014 at 05:49:30 UTC, Ali Çehreli wrote:
> Can you come up with a D library solution to the following C++11 proposal:
>
>   http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2014/n3950.html
>

did this years ago (or something similar, at least):
https://bitbucket.org/ariovistus/multi_index/src/0993f8604c1438f6d9f26f580939b42fdd159a67/src/multi_index.d?at=default#cl-4654

example:

https://bitbucket.org/ariovistus/multi_index/src/0993f8604c1438f6d9f26f580939b42fdd159a67/unittests/multi_compare.d?at=default