Thread overview | ||||||
---|---|---|---|---|---|---|
|
September 29, 2008 D2: how to cast away const/invariant in template? | ||||
---|---|---|---|---|
| ||||
Using D2: given a template with parameter T, how do you strip away const/invariant from the type T in an overload without creating a separate overload for each and every possible type? What I'm looking for is something like: T strip( T )( T arg ) { return arg; } T strip( T: const T )( const T arg ) { return cast(T) arg; } T strip( T: invariant T )( invariant T arg ) { return cast(T) arg; } The above doesn't work but should be enough to show what I'm asking. Thanks, Brian |
September 30, 2008 Re: D2: how to cast away const/invariant in template? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brian Price | On Mon, 29 Sep 2008 19:38:42 +0000 (UTC), Brian Price
<blprice61@yahoo.com> wrote:
>Using D2: given a template with parameter T, how do you strip away const/invariant from the type T in an overload without creating a separate overload for each and every possible type?
>
>What I'm looking for is something like:
>
>T strip( T )( T arg )
>{
> return arg;
>}
>
>T strip( T: const T )( const T arg )
>{
> return cast(T) arg;
>}
>
>T strip( T: invariant T )( invariant T arg )
>{
> return cast(T) arg;
>}
>
>The above doesn't work but should be enough to show what I'm asking.
>Thanks,
>Brian
Your example doesn't work because of a compiler bug. You could use std.traits.Mutable template or its implementation:
import std.traits;
auto strip(T)(T arg)
{
return cast(Mutable!(T)) arg;
}
or
auto strip(T)(T args)
{
static if (is(T U == const(U)) || is(T U == invariant(U)))
return cast(U) args;
else
return args;
}
|
September 30, 2008 Re: D2: how to cast away const/invariant in template? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | On Tue, 30 Sep 2008 09:49:26 +0300, Max Samukha <samukha@voliacable.com.removethis> wrote: >On Mon, 29 Sep 2008 19:38:42 +0000 (UTC), Brian Price ><blprice61@yahoo.com> wrote: > >>Using D2: given a template with parameter T, how do you strip away const/invariant from the type T in an overload without creating a separate overload for each and every possible type? >> >>What I'm looking for is something like: >> >>T strip( T )( T arg ) >>{ >> return arg; >>} >> >>T strip( T: const T )( const T arg ) >>{ >> return cast(T) arg; >>} >> >>T strip( T: invariant T )( invariant T arg ) >>{ >> return cast(T) arg; >>} >> >>The above doesn't work but should be enough to show what I'm asking. >>Thanks, >>Brian > >auto strip(T)(T args) >{ > static if (is(T U == const(U)) || is(T U == invariant(U))) > return cast(U) args; > else > return args; >} args was meant to be arg |
September 30, 2008 Re: D2: how to cast away const/invariant in template? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | The std.traits.Mutable solution is exactly what I needed. Thanks, Brian |
Copyright © 1999-2021 by the D Language Foundation