Slides from my ACCU Silicon Valley talk

biozic dransic at free.fr
Tue Dec 14 10:27:23 PST 2010


Le 14/12/10 16:50, Andrei Alexandrescu a écrit :
> On 12/14/10 7:33 AM, biozic wrote:
>> Le 14/12/10 09:56, Andrei Alexandrescu a écrit :
>>> On 12/14/10 2:23 AM, 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.
>>>
>>> Yah, I was planning on writing the same response.
>>>
>>> Andrei
>>
>> I have a question about this and some pieces of code in the standard
>> library, notably std.algorithm: some of the templated functions use
>> template constraints even when no template overloading is taking place.
>> Wouldn't some static asserts help print more accurate messages when
>> these functions are misused?
>>
>> Nicolas
>
> The intent is to allow other code to define functions such as e.g. 
> "map" or "sort". Generally any generic function should exclude via a 
> constraint the inputs it can't work on. That way no generic function 
> bites off more than it can chew.
>
> Andrei
>
Yes, I understand that this is more important than the accuracy of error 
messages.
Thanks



More information about the Digitalmars-d mailing list