www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D2 cast away const

reply Frank Benoit <keinfarbton googlemail.com> writes:
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
parent reply "Janice Caron" <caron800 googlemail.com> writes:
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
parent Robert DaSilva <spunit262.nospam gmail.com> writes:
Janice Caron wrote:
 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.

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.
Nov 07 2007