core.stdc.stdlib._compare_fp_t and qsort

Dmitry Olshansky dmitry.olsh at gmail.com
Sun Mar 18 13:10:08 UTC 2018


On Sunday, 18 March 2018 at 11:29:47 UTC, Joe wrote:
> On Monday, 12 March 2018 at 03:50:42 UTC, Joe wrote:
>> On Monday, 12 March 2018 at 03:13:08 UTC, Seb wrote:
>>> Out of interest: I wonder what's your usecase for using 
>>> qsort. Or in other words: why you can't use the high-level 
>>> std.algorithm.sorting.sort?
>>
>> This is only temporary. I will be using 
>> std.algorithm.sorting.sort. I was converting a C program and 
>> it annoyed me that I couldn't get the qsort invocation past 
>> the D compiler.
>
> Now that I'm trying to use std.algorithm.sorting, I'm again 
> puzzled by what I need to use for the "less" predicate. My 
> first try was:
>
>    sort!((a, b) => to!string((*a).name) < 
> to!string((*b).name))(recs);
>
> This results in the error:
>
> Error: template std.algorithm.sorting.sort cannot deduce 
> function from argument types !((a, b) => to!string((*a).name) < 
> to!string((*b).name))(Record*[10]),

This basically says you have fixed size array, in D they don’t 
decay to slices/pointers for safety reasons (it’s stack memory 
that is easy to leak out of scope with disasterous consequences).

Do this to get the usual ptr + length:

sort!((a, b) => to!string((*a).name) < 
to!string((*b).name))(recs[]);

Also to!string would be computed on each compare anew. May want 
to use schwartzSort to avoid that, on 10 elements there is no 
real difference though.



More information about the Digitalmars-d-learn mailing list