Resolving conflicting functions
Bill Baxter
dnewsgroup at billbaxter.com
Mon Dec 3 18:23:18 PST 2007
BCS wrote:
> Reply to Derek,
>
>> I have a Currency data type,based on a struct.
>>
>> I want to divide a Currency value by a scalar to return a Currency and
>> I want to divide a Currency value by another Currency value to return
>> a scalar. This below is my attempt but fails to compile. How does one
>> do this?
>>
>> // ------------------------
>> real opDiv(Currency pFactor)
>> // ------------------------
>> {
>> return mData / pFactor.mData;
>> }
>> // ------------------------
>> Currency opDiv(T)(T pFactor)
>> // ------------------------
>> {
>> Currency temp;
>> temp = this.mData / pFactor;
>>
>> return temp;
>> }
>> I get the message ...
>>
>> template Currency.opDiv(T) conflicts with function Currency.opDiv
>>
>
> you can't have a template and non-template by the same name
>
> here is the nasty solution, ther might be a better one, but this should
> work.
>
> // ------------------------
> Ret!(T) opDiv(T)(T pFactor)
> // ------------------------
> {
> Ret!(T) temp;
> static if(is(T == Currency))
> temp = mData / pFactor.mData;
> else
> temp = this.mData / pFactor;
>
> return temp;
> }
I think in D2 you should be able to use typeof(return) to remove the
need for the extra helper template, Ret.
Totally untried though.
// ------------------------
typeof(return) opDiv(T)(T pFactor)
// ------------------------
{
static if(is(T == Currency))
return mData / pFactor.mData;
else
return this.mData / pFactor;
}
--bb
More information about the Digitalmars-d-learn
mailing list