Template question

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Thu Feb 22 13:22:02 PST 2007


Michiel wrote:
> 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?

What's wrong with "auto x = new Set!(int);"? It's how every other class 
type is instantiated.

An alternative would be to make your set type a struct instead of a 
class, then you could just have "Set!(int) x;" declare an empty set.
You'd have to be careful about the copying behavior though, structs are 
copied by value, not reference. But if the struct contained a 
reference/pointer to the actual data, it could work.

> 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.

This would probably be a bad idea in a statically typed language. It 
could be made to work, but that would require you to specify the type 
whenever you wanted to extract an element from the set...



More information about the Digitalmars-d mailing list