Template issues in D

Burton Radons burton-radons at shaw.ca
Wed Feb 20 14:01:43 PST 2008


Bill Baxter Wrote:

> ------------------------
> 2) Lack of Overloading:
> 
> The second big category of headaches comes from lack of overloading.
> 
> 2a) Functions and templates.
> You can't have a function and a template with the same name.  C++ has no 
> problem with this.  I don't see why D should.  The workaround isn't 
> terribly onerous, but then again neither should it be a terribly 
> difficult fix, I suspect.
> 
> 2b) Templates and templates
> This is worse.  You can't overload templates with other templates of the 
> same # of parameters even if the signatures are unambiguious.  This is 
> where the infamous "dummy=void" hack comes in.
> 
> 2c) No ADL:
> 
> Not being able to overload templates across modules kills user 
> extensibility of templates.  It's a very common pattern in C++ code.  A 
> generic class in a library uses Traits<T> to get info about a class.  If 
> you make a new MyT, you can integrate your class into the lib by 
> providing your own specialization of Traits!(MyT).  In D to get this you 
> have to modify the original module where Traits! is defined.
> 
> It's like saying that to extend a class from a library you should edit 
> the library's source code and add a member to the class there.  It is 
> the antithesis of modular code.
> 
> I hope this will be fixed in D2 by the overload sets stuff, but I 
> consider D1 broken for lack of this.

I'm not so sure I want the language to act differently just for templates. What I would like is for specialisations to be predicatable. That would not only give us the power of extensible templates, but it would allow new features as well. So I would use:
	
	// Or whatever you'd use to get the values of an array.
	T add_arrays (T = is_array! (T) && is_numeric! (array_values! (T))) (T a, T b)
	{
	}

I would be very happy with that, and the compiler could specially understand that so might fail it with "array_add cannot be used with foo because it failed the is_array predicate." We could be doing very different things with templates though so that might not work for you.

I'll read about your 3rd thing when I have time.



More information about the Digitalmars-d mailing list