www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Phobos, std.typecons: arrays of Rebindable - with DOS file

pc Wrote:

 pc Wrote:
 
 I had problems sorting an array of Rebindable!(..).   Please see attached
file.  
 
 Am I completely missing the point or is this a bug? Also, a shown in the file,
a hack to make this work may depend on a bug? in std.algoritm's swap function.
But I am way over my head. Just trying to help.  
import std.traits; import std.stdio, std.algorithm; class C1 { int x; this(int x){this.x = x;} int y() const { return x; } } alias Rebindable!(const(C1)) RC1; /*** Bugs?? -- Sorting and array of, say, Rebindable!(const(C1)) RC1; Possible typecons.d Bug Before I changed opAssign in a copy of Rebindable, below, test3 would not compile. I.e., Arrays of Rebindable appeared to fail for sorting because swap was not working. Possible algorithm.d Bug After the change to opAssign, everything worked just worked the change it just worked. But maybe it should not have worked so easily. I.e, after reading 1.8 of TDPL and looking breifly at the code in std.algorithm, I thought that I would have to ?overload? swap: void swap(ref RC1 a, ref RC1 b) { auto t = a; b = a; a = t; } But it worked without this. Is this correct behavior? */ void test3() { RC1[] aCs; aCs.length = 2; aCs[0] = new C1(9); aCs[1] = new C1(10); aCs ~= RC1(new C1(8)); sort!("a.x < b.x")(aCs); foreach(ac; aCs) writeln(ac.y()); } // lifted from typecons.d // except for second opAssign template Rebindable(T) if (is(T : Object) || isArray!(T)) { static if (!is(T X == const(U), U) && !is(T X == invariant(U), U)) { alias T Rebindable; } else static if (isArray!(T)) { alias const(ElementType!(T))[] Rebindable; } else { struct Rebindable { private union { T original; U stripped; } void opAssign(T another) { stripped = cast(U) another; } // ---- comment out this opAssign and test3 will not compile void opAssign(Rebindable another) { stripped = another.stripped; } // ----- static Rebindable opCall(T initializer) { Rebindable result; result = initializer; return result; } alias original get; T opDot() { return original; } } } } void main(){ test3(); }
Dec 26 2009