Segmentation fault on sort

bearophile bearophileHUGS at lycos.com
Fri Jan 3 11:59:40 PST 2014


Dfr:

> struct Color {
>   string fg;
>   string bg;
>   string attrs;
>   int slid;
> }
>
> auto tt = [Tuple!(uint, Color)(28, Color("0", "255", "", 0)), 
> Tuple!(uint, Color)(28, Color("0", "255", "", 0))];
> tt.sort;
>
> This just dies on "tt.sort", any idea what is wrong ?

Your code has two problems: you are using the buggy built-in sort 
instead of the Phobos one, and you have not defined an opCmp and 
opEquals in Color.

If you don't want to define those two methods you can use a tuple 
again:


import std.stdio, std.algorithm, std.typecons;

struct Color0 {
     string fg, bg, attrs;
     int slid;
}

alias Color = Tuple!(string,"fg", string,"bg", string,"attrs", 
int,"slid");

void main() {
     alias T = Tuple!(uint, Color);
     auto tt = [T(28, Color("0", "255", "", 0)),
                T(28, Color("0", "255", "", 0))];

     tt.writeln;
     tt.sort(); // Phobos sort.
     tt.writeln;
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list