Bug 17982 – Support for const(Class) in algorithm.searching.extremum
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2017-11-14T09:31:49Z
Last change time
2018-04-05T06:20:31Z
Keywords
pull
Assigned to
No Owner
Creator
Aurelien Fredouelle
Comments
Comment #0 by aurelien.fredouelle+dlang — 2017-11-14T09:31:49Z
Initially reported in https://forum.dlang.org/post/[email protected]
It is not possible to use minElement on an array of const objects:
class A
{
int val;
}
const(A) doStuff(const(A)[] v)
{
import std.algorithm.searching : minElement;
return v.minElement!"a.val";
}
This gets the following compiler error:
std/algorithm/searching.d(1256,28): Error: cannot implicitly convert expression (front(r)) of type const(A) to app.A
std/algorithm/searching.d(1286,35): Error: cannot implicitly convert expression (r[i]) of type const(A) to app.A
std/algorithm/searching.d(1258,36): Error: template instance std.algorithm.searching.extremum!("a.val", "a < b", const(A)[], A) error instantiating
std/algorithm/searching.d(3345,24): instantiated from here: extremum!("a.val", "a < b", const(A)[])
source/app.d(11,11): instantiated from here: minElement!("a.val", const(A)[])
This seems to be because the extremum helper function is using Unqual!Element to hold the current extremum, which cannot be assigned to when Element is const.
In the forum thread, @vit suggested using something like this instead:
template RebindableOrUnqual(T){
static if (is(T == class) || is(T == interface) || isDynamicArray!T || isAssociativeArray!T)alias RebindableOrUnqual = Rebindable!T;
else alias RebindableOrUnqual = Unqual!T;
}