www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.algorithm.sort with copy constructors

reply Gregor =?UTF-8?B?TcO8Y2ts?= <gregormueckl gmx.de> writes:
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
Apr 13 2020
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 4/13/20 9:27 AM, Gregor Mückl wrote:
 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);
 }
This is a bug in phobos. https://issues.dlang.org/show_bug.cgi?id=20732 Also went down a rabbit hole due to this: https://issues.dlang.org/show_bug.cgi?id=20733 -Steve
Apr 13 2020
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 4/13/20 10:24 AM, Steven Schveighoffer wrote:
 This is a bug in phobos.
 
 https://issues.dlang.org/show_bug.cgi?id=20732
https://github.com/dlang/phobos/pull/7442 -Steve
Apr 13 2020
parent Gregor =?UTF-8?B?TcO8Y2ts?= <gregormueckl gmx.de> writes:
On Monday, 13 April 2020 at 15:38:33 UTC, Steven Schveighoffer 
wrote:
 On 4/13/20 10:24 AM, Steven Schveighoffer wrote:
 This is a bug in phobos.
 
 https://issues.dlang.org/show_bug.cgi?id=20732
https://github.com/dlang/phobos/pull/7442 -Steve
Sorry for sending you down that particular rabbit hole. And a big thank you for following through! I worked around that problem in my use case, but it's good to see one a couple of bugs squashed. :)
Apr 14 2020