Template Parameter Deduction
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Mar 11 16:04:15 PDT 2015
On 03/11/2015 03:44 PM, Paul D Anderson wrote:
> This used to work in D2.065:
>
> given
>
> 1) public T mul(T)(in T x, in T y,
> Context context = T.context) if (isDecimal!T)
> // one template parameter for the two input values
>
> and
>
> 2) public T mul(T, U)(in T x, U n, Context context = T.context)
> if (isDecimal!T && isIntegral!U)
> // two different template parameters for the two input values
>
> then
>
> 3) dec9 arg1 = dec9("1.20");
> long arg2 = 3;
> result = mul(arg1, arg2);
> // correctly deduced function
>
> But now (D2.066.1) either 1) has to be changed to
>
> 1) public T mul(T, U)(in T x, U y, Context context = T.context)
> if (isDecimal!T && isDecimal!U)
> // two identical template parameters for the two input values
>
> or 3) has to be changed to
>
> 3) dec9 arg1 = dec9("1.20");
> long arg2 = 3;
> result = mul!(dec9,long)(arg1, arg2);
> // template parameters have to be made explicit
>
> Is this expecded behavior?
>
> Paul
Hint: It makes it much simpler to work with complete code. The following
code took a while for me to put together:
template isDecimal(T)
{
enum isDecimal = true;
}
template isIntegral(T)
{
enum isIntegral = true;
}
public T mul(T)(in T x, in T y,
Context context = T.context) if (isDecimal!T)
// one template parameter for the two input values
{
return x;
}
alias Context = int;
public T mul(T, U)(in T x, U n, Context context = T.context)
if (isDecimal!T && isIntegral!U)
// two different template parameters for the two input values
{
return x;
}
struct dec9
{
string s;
enum context = 42;
}
void main()
{
dec9 arg1 = dec9("1.20");
long arg2 = 3;
dec9 result = mul(arg1, arg2);
// correctly deduced function
}
Yes, it fails with 2.066.1 but compiles fine with git head.
Ali
More information about the Digitalmars-d-learn
mailing list