Resolving conflicting functions
BCS
ao at pathlink.com
Mon Dec 3 16:57:59 PST 2007
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;
}
More information about the Digitalmars-d-learn
mailing list