Template question

BCS ao at pathlink.com
Thu Feb 22 13:08:56 PST 2007


Reply to Michiel,

> I'm not yet familiar enough with the template system to do this.
> 
> I wrote the following function:
> 
> ----------------------------------------
> Set!(T[0]) set(T ...)(T elements) {
> auto result = new Set!(T[0]);
> foreach (element; elements) {
> result.add(element);
> }
> return result;
> }
> ----------------------------------------
> This gives a nice set literal syntax with implicit type deduction.
> 
> auto x = set(4, 5, 6, 99);
> 
> But the user of the function should also have the option to use it
> like this:
> 
> auto x = set!(int);

would this work?

auto x = Set!(int);

it's not quite as clean but...


If you don't like that 

you could try having set used as above take 2 things plus a tuple, then have 
a version of the template that takes one type and has function that take 
one or zero arguments

Set!(T) set(T, U, V...)(T e1, U e2, V elements) {...}

template set(T)
{
Set!(T) set(T e1) {...}
Set!(T) set() {...}
}

again, not as clean but, it could work.

I haven't tried this so it may not work.





More information about the Digitalmars-d mailing list