Parameter specialization

Artur Skawina art.08.09 at gmail.com
Sat Jul 21 00:37:53 PDT 2012


On 07/21/12 08:29, Ali Çehreli wrote:
> On 07/20/2012 06:47 PM, Eyyub wrote:
>> I have a question about the semantic of parameter
>> specialization(TemplateTypeParameterSpecialization)
>> In this following code, there are 2 forms of the same function 'add' :
>> <code>
>>
>> T add(T, U : T) (T a, U b) //doesn't work
>> {
>> return a + b;
>> }
>>
>> T add(T, U) (T a, U b) if(is(U : T)) //works
>> {
>> return a + b;
>> }
>>
>>
>> void main()
>> {
>> assert(add(2, cast(short)2) == 4);
>> }
>> </code>
>> So, I infer that, in this case, TemplateTypeParameterSpecialization and
>> TypeSpecialization(of IsExpression) aren't semantically equal ? What are
>> differences between this 2 forms ?
> 
> The confusion is due to the two different meanings of the ':' operator.
> 
> 1) In the template parameter list, ':' specializes U. "U : T" means: "this is a specialization where U is T".
> 
>   http://dlang.org/template.html
> 
> (Search for the "Specialization" section.)
> 
> When you use the template, T is int and U is short; so that specialization is not considered.

No; it would be considered. The problem is likely related to "Function template
type parameters that are to be implicitly deduced may not have specializations".

Note that

   T add(T, U : int) (T a, U b) {...}

already works, and "add(T, U : T)" could probably be made to work too.

artur


More information about the Digitalmars-d-learn mailing list