CTFE sort of tuples

Andrey saasecondbox at yandex.ru
Thu May 2 02:54:03 UTC 2019


Hello, I have got this code:

> alias Group = Tuple!(int[], "data", int, "key");
> 
> void main()
> {
>     enum group = [
>     	Group([1,2,3,4], 1),
>     	Group([5,3], 1),
>     	Group([4,5,4, 8, 9, 4], 1),
>     	Group([2,3,4], 1),
>     ];
> 
>     enum result = group.sort!"a.data < b.data"().array(); }

I want to sort array of tuples using "data" element in CTFE. But 
this code give me errors:
> /dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/mutation.d(2782): Error: reinterpreting cast from Tuple!(int[], "data", int, "key")* to ubyte* is not supported in CTFE
> /dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/mutation.d(3014):        called from here: swap(r[i1], r[i2])
> /dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/sorting.d(1672):        called from here: swapAt(r, 2LU, 3LU)
> /dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/sorting.d(2112):        called from here: shortSort(r)
> /dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/sorting.d(1875):        called from here: quickSortImpl(r, r.length)
> onlineapp.d(32):        called from here: sort([Tuple([1, 2, 3, 
> 4], 1), Tuple([5, 3], 1), Tuple([4, 5, 4, 8, 9, 4], 1), 
> Tuple([2, 3, 4], 1)])
> onlineapp.d(32):        called from here: array(sort([Tuple([1, 
> 2, 3, 4], 1), Tuple([5, 3], 1), Tuple([4, 5, 4, 8, 9, 4], 1), 
> Tuple([2, 3, 4], 1)]))

As I understand the function "sort" sometimes can't be run at CT 
because of reinterpreting cast.
In this case how to sort?


More information about the Digitalmars-d-learn mailing list