Slides from my ACCU Silicon Valley talk

Jonathan M Davis jmdavisProg at gmx.com
Tue Dec 14 00:49:10 PST 2010


On Tuesday 14 December 2010 00:23:58 Walter Bright wrote:
> bearophile wrote:
> > Andrei:
> >> D's constrained templates were introduced on 17 June 2008 and they've
> >> been a smashing hit. Virtually all of Phobos uses them, and I'm sure a
> >> lot of generic client code. They take half a minute to explain and
> >> solve a difficult problem in a very simple manner.<
> > 
> > A template with two constrains makes it less handy to add error messages
> > that
> 
> > explain why the instantiation has failed:
> Adding such error messages misses the point of constraints. The idea is
> that template overloading is based on constraints. Given 3 templates with
> 3 constraints, overload resolution picks the one with constraints that
> pass. Giving error messages for the others is wrong.
> 
> If you want it to produce error messages if the instantiation is wrong, the
> correct way is to use static asserts within the template body. No
> additional features serve any purpose.

It could definitely serve a purpose if _all_ of the possible template 
specializations fail. Presumably, you intend _one_ of them to work. But that 
_does_ mean that you get error messages for a number of different template 
specializations, and you have to sort through them. It _can_ take a fair bit of 
effort to figure out why a particular template constraint is failing, and it would 
definitely be nice if there were more information on _why_ a template constraint 
fails.

However, I do agree that doing so could get rather complicated, and that 
ultimately, it probably isn't worth it. I fully expect, however, that many 
people in the future will complain that they can't get better error messages 
which indicate why a particular template constraint failed.

- Jonathan M Davis


More information about the Digitalmars-d mailing list