Hmm - about manifest/enum

Walter Bright newshound1 at digitalmars.com
Tue Jan 1 02:16:44 PST 2008


Steven Schveighoffer wrote:
> How about doing an in-place sort of an array of const classes?

Good question, and one that I wrestled with a lot. The answer is, just 
like with strings, build another array.

> const(C)[] myarray;
> 
> ...
> // swap two items i and j
> auto tmp = myarray[i];
> myarray[i] = myarray[j]; // error
> myarray[j] = tmp; // error
> ...
> 
> Before you answer, building another array isn't acceptable, because that is 
> a waste of resources and adds runtime.

That's what our C intuition tells us, and that's why I resisted 
immutable strings for so long. But the way caching works on modern 
processors, our intuition about what is fast and slow about memory 
operations often is wrong. Furthermore, the bug resistance of immutable 
strings I believe more than makes up for it.


> Tell me how you do this without having tail-const for class references.  I 
> couldn't care less about tail-const for structs, or tail-const for class 
> instances (being defined as a state where the members of the class/struct 
> are mutable, but any objects pointed to by its members are const).  This 
> feature of being able to swap two references to const data is a very common 
> algorithmic requirement.

You're right, you cannot do it without tail-const for class references. 
But if you're desperate, you can make it work using casting.



More information about the Digitalmars-d mailing list