Template question

Michiel nomail at please.com
Thu Feb 22 13:38:35 PST 2007


Michiel wrote:
> BCS wrote:
> 
>> would this work?
>>
>> auto x = Set!(int);
>>
>> it's not quite as clean but...
> 
> It would have to be:
> 
> auto x = new Set!(int);
> 
>> 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.
> 
> I don't really care if the function implementation isn't very clean, as
> long as it's clean where set literals are used. That's what you want
> from a library.
> 
> I'll try it out and get back to you.

Hm. No go. I implemented it like this:

-----------------------------------------------
Set!(T) set(T, U, V ...)(T e1, U e2, V rest) { // line 287
	auto result = new Set!(T);
	
	result.add(e1);
	result.add(e2);
	foreach (element; rest) {
		result.add(element);
	}
	
	return result;
}

template set(T) {
	Set!(T) set() {
		return new Set!(T);
	}
	
	Set!(T) set(T e1) {
		auto result = new Set!(T);
		
		result.add(e1);
		
		return result;
	}
}

-----------------------------------------------

But got this error:

set.d(287): template tools.Set.set(T,U,V...) is not a function template

It's because of the existence of the second template. I removed it and
the first would work. I also tried to rewrite the last two functions
like this:

-----------------------------------------------
Set!(T) set(T)() {
	return new Set!(T);
}

Set!(T) set(T)(T e1) { // line 303
	auto result = new Set!(T);
	
	result.add(e1);
	
	return result;
}
-----------------------------------------------

But got this error:

set.d(303): template tools.Set.set(T) conflicts with
tools.Set.set(T,U,V...) at set.d(287)

Which is strange, because it shouldn't conflict at all. A bug in dmd,
perhaps?

Thanks for the suggestion, anyway! I hadn't thought of it.

-- 
Michiel



More information about the Digitalmars-d mailing list