Double face templates

BCS ao at pathlink.com
Mon Sep 29 14:30:00 PDT 2008


Reply to bearophile,

> I have created several templates, like one very simple named
> ArrayType1 that given the type of an array becomes an alias of the
> type of its items (there are other more complex templates too).
> 
> They can be used as:
> ArrayType1!(TypeArray)
> or:
> ArrayType1!(typeof(somearray))
> But to reduce code clutter if possible I'd like to create a template
> that works in both cases:
> ArrayType1!(TypeArray)
> ArrayType1!(somearray)
> I know template "arguments" can be an alias too, so I may define a
> pair like this:
> 
> template ArrayType1!(T) {...}
> 
> template ArrayType1!(alias x) { ... }
> 
> but when I define both templates I always enter a swamp, with so many
> (sometimes strange) bugs that so far I have always thrown away the
> template version with alias and I have kept only the more normal
> template defined on a type.
> 
> Is what I am trying to do wrong/ unsafe/ philosophically bad? If my
> purpose isn't bad, then can you suggest how to implement it much more
> reliably?
> 
> Thank you,
> bearophile

you might be able to use a form like this

template T(alias a)
{
    static if(is(a))
    {
       // stuff
    }
    else
       alias T!(typeof(a)) T;
    // or 'mixin T!(typeof(a));'
}




More information about the Digitalmars-d-learn mailing list