std.algorithm.sort with copy constructors

Gregor Mückl gregormueckl at gmx.de
Mon Apr 13 13:27:09 UTC 2020


Hi!

Consider the following code:

---

import std;

struct S {
     pure this(ref return scope const S rhs) nothrow @nogc {
         this.x = x;
     }

     int x;
}

void main()
{
     S[] array = new S[10];
     array.sort!("a.x < b.x", SwapStrategy.stable);
}

---

In this program, sort compiles only if the copy constructor is 
decorated with pure, nothrow and @nogc. This is very limiting. Is 
there a way to get rid of nothrow and @nogc on the copy 
constructor and still use sort?

My actual use case, inefficient as it may be, is to sort structs 
that use copy constructors to enforce deep copies of their 
contents. So there's liberal use of .dup in them, which obviously 
is neither nothrow nor @nogc.

Cheers,
Gregor



More information about the Digitalmars-d-learn mailing list