On 07/20/2012 06:47 PM, Eyyub wrote:
Hello,

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 ?

Eyyub,

(I hope that I have an english a bit compréhensible)

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.

2) In an is expression, ':' means "implicitly convertible to".

  http://dlang.org/expression.html#IsExpression

So "is(U : T)" matches because "short is implicitly convertible to int."

Ali

--
D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html

Reply via email to