Calling assumeSorted on const(std.container.Array)

Olivier Prat o.prat at free.fr
Sun Mar 26 16:03:28 UTC 2023


On Sunday, 26 March 2023 at 02:16:15 UTC, Steven Schveighoffer 
wrote:
> On 3/25/23 9:45 AM, Olivier Prat wrote:
>
>> [...]
>
> It's because a Range keeps a copy of the array (Array is a 
> reference counted type). Since that is labeled `const`, then 
> editing the Range is forbidden.
>
> Inside SortedRange, it has this, which is causing the issue:
>
> ```d
>     static if (isForwardRange!Range)
>     @property auto save()
>     {
>         // Avoid the constructor
>         typeof(this) result = this;
>         result._input = _input.save;
>         return result;
>     }
> ```
>
> Overwriting the input isn't possible here, because it contains 
> a const member.
>
> Now, this possibly could be fixed with something like:
>
> `return typeof(this)(_input.save);`
>
> But it might just push the error to another place.
>
> The whole thing is kind of ugly.
>
> There is a note inside the Array Range which says it's trying 
> to work around some old bug that is now marked as "works for 
> me", so maybe that can be reexamined.
>
> https://github.com/dlang/phobos/blob/17b1a11afd74f9f8a69af93d77d4548a557e1b89/std/container/array.d#L137
>
> -Steve

Thanks to both of you for the answers. It confirms my 
investigation and my workaround is to cast the const ranges into 
their non const versions, when I encounter these issues. Not very 
pretty.


More information about the Digitalmars-d-learn mailing list