Thread overview
std.algorithm.sort with copy constructors
Apr 13, 2020
Gregor Mückl
Apr 15, 2020
Gregor Mückl
April 13, 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

April 13, 2020
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
April 13, 2020
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
April 15, 2020
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. :)