Thread overview | ||||||
---|---|---|---|---|---|---|
|
February 15, 2009 Template conflict? | ||||
---|---|---|---|---|
| ||||
Can anybody explain why I get the following error: test.d(28): Error: test.tblahTemplate!(uint).tblah at test.d(13) conflicts with test.tblahTemplate!(int).tblah at test.d(13) When I try to compile this?: test.d: int blah(uint x) { return x; } int blah(int x) { return (x >= 0 ? x : -x); } version(WithT) { template tblahTemplate(Type) { Type tblah(Type x) { static if(Type.min == 0) return x; else return (x >= 0 ? x : -x); } } mixin tblahTemplate!(uint); mixin tblahTemplate!(int); } void main() { blah(3); version(WithT) { tblah(3); } } It works without -version=WithT but not with it. Why can't it resolve it if it's a template? Thanks, Mike L. |
February 15, 2009 Re: Template conflict? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike L. | On Sun, 15 Feb 2009 02:20:50 -0500, Mike L. wrote: > Can anybody explain why I get the following error: > > test.d(28): Error: test.tblahTemplate!(uint).tblah at test.d(13) conflicts with > test.tblahTemplate!(int).tblah at test.d(13) > tblah(3); It fails because of the literal '3'. The compiler cannot be sure if you want the 'int' or 'uint' function called because '3' matches both of them. You need to make the literal explicit... tblah(3u); -- uint call tblah(cast(int)3); -- int call -- Derek Parnell Melbourne, Australia skype: derek.j.parnell |
February 15, 2009 Re: Template conflict? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | Derek Parnell Wrote:
> On Sun, 15 Feb 2009 02:20:50 -0500, Mike L. wrote:
>
> > Can anybody explain why I get the following error:
> >
> > test.d(28): Error: test.tblahTemplate!(uint).tblah at test.d(13) conflicts with
> > test.tblahTemplate!(int).tblah at test.d(13)
>
> > tblah(3);
>
> It fails because of the literal '3'. The compiler cannot be sure if you want the 'int' or 'uint' function called because '3' matches both of them.
>
> You need to make the literal explicit...
>
> tblah(3u); -- uint call
> tblah(cast(int)3); -- int call
>
>
> --
> Derek Parnell
> Melbourne, Australia
> skype: derek.j.parnell
But what confuses me is that it needs it for the template, but not when I create blah() without templates. Why the inconsistency?
|
February 15, 2009 Re: Template conflict? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike L. |
Mike L. wrote:
> Derek Parnell Wrote:
>
>> On Sun, 15 Feb 2009 02:20:50 -0500, Mike L. wrote:
>>
>>> Can anybody explain why I get the following error:
>>>
>>> test.d(28): Error: test.tblahTemplate!(uint).tblah at test.d(13) conflicts with
>>> test.tblahTemplate!(int).tblah at test.d(13)
>>> tblah(3);
>> It fails because of the literal '3'. The compiler cannot be sure if you want the 'int' or 'uint' function called because '3' matches both of them.
>>
>> You need to make the literal explicit...
>>
>> tblah(3u); -- uint call
>> tblah(cast(int)3); -- int call
>>
>>
>> --
>> Derek Parnell
>> Melbourne, Australia
>> skype: derek.j.parnell
>
> But what confuses me is that it needs it for the template, but not when I create blah() without templates. Why the inconsistency?
It's a side-effect of the mixins. AFAIK, the compiler is treating those mixins as coming from different sources. When it encounters multiple symbols from different sources, it's designed to be overly cautious and assume you didn't know.
I think you can override this by changing the two mixin lines to this:
alias tblahTemplate!(uint) tblah;
alias tblahTemplate!(int) tblah;
-- Daniel
|
Copyright © 1999-2021 by the D Language Foundation