Template question

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Thu Feb 22 15:16:41 PST 2007


Daniel Keep wrote:
> SetOf!(T) set(T...)(T elements)
> {
>     auto result = new SetOf!(T);
> 
>     foreach( element ; elements )
>         result.add(element);
> 
>     return result;
> }
> 
> template SetOf(T...)
> {
>     static if( is( typeof(T[0]) ) )
>         alias Set!(typeof(T[0])) SetOf;
>     else
>         alias Set!(T[0]) SetOf;
> }
> 
> Obviously, this only works if Set!(T) expects "T" to be an actual type.

(Note: I haven't actually tried that code)

First I'd like to suggest also skip adding the first element if it was a 
type.

Second, if your SetOf doesn't work you might also try removing the 
typeof from the condition and switching the two clauses. IIRC is(x) is 
also false for any x that isn't a type.

> Note that I haven't tried the above.  This should *theoretically* work
> since taking the type of a type makes no sense (hence, the is expression
> should fail), whilst taking the type of a value does.

I seem to remember typeof(myType) being the type aliased to myType. I 
don't know why, but I do seem to recall that being the case...

> If it doesn't work, blame it on just having woken up :P

Ditto, except in my case blame it on it being after midnight here :P.



More information about the Digitalmars-d mailing list