digitalmars.D - D2 cast away const
- Frank Benoit (8/8) Nov 05 2007 Is there a way to make a generic T const free?
- Janice Caron (6/7) Nov 05 2007 I think this works
- Robert DaSilva (12/23) Nov 07 2007 Almost,
Is there a way to make a generic T const free? E.g. there is this function: T[] reverse(T)(T[] str ){...} auto res1 = reverse( "abc".dup ); // T=char auto res2 = reverse( "abc" ); // T=invariant(char) If reverse now needs a mutable T in its implementation, how can it be implemented? Without explicitely testing for the known types char/wchar/dchar?
Nov 05 2007
On 11/5/07, Frank Benoit <keinfarbton googlemail.com> wrote:Is there a way to make a generic T const free?I think this works template(T) { typedef T Mutable; } template(T:const(T)[]) { typedef T[] Mutable; } Then Mutable!(T) is the mutable equivalent of T That's from memory, but I've had something like that working.
Nov 05 2007
Janice Caron wrote:On 11/5/07, Frank Benoit <keinfarbton googlemail.com> wrote:Almost, template Mutable(T) { alias typeof(T) Mutable; } template Mutable(T : T[]) { alias typeof(T)[] Mutable; } typedef creates a new distinct type, and for some reason you need to use typeof to remove the invariantness from T.Is there a way to make a generic T const free?I think this works template(T) { typedef T Mutable; } template(T:const(T)[]) { typedef T[] Mutable; } Then Mutable!(T) is the mutable equivalent of T That's from memory, but I've had something like that working.
Nov 07 2007