Template question

Michiel nomail at please.com
Thu Feb 22 12:57:11 PST 2007


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);

To create the empty set (that can hold int). How can I implement this?

Thanks!

PS: I would have liked to skip the type altogether for the empty set,
but the problem appears when you want to put something inside that set.
I've considered using a dummy type and to somehow construct the correct
set-type after the first insert, but it wouldn't be consistent with
normal sets and altogether too much trouble.

-- 
Michiel



More information about the Digitalmars-d mailing list