www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/05/12/03:06:00

Date: Thu, 11 May 1995 10:00:17 -0700 (PDT)
From: Gordon Hogenson <ghogenso AT u DOT washington DOT edu>
To: "A.Appleyard" <A DOT APPLEYARD AT fs2 DOT mt DOT umist DOT ac DOT uk>
Cc: DJGPP AT SUN DOT SOE DOT CLARKSON DOT EDU
Subject: Re: pow()

On Thu, 11 May 1995, A.Appleyard wrote:

> #include<math.h>
> main(){int i,j; double x,y;
> pow(i,j);
> pow(x,i);
> pow(x,y);}
> 
> 
> C:\WORK>c:\djgpp\bin\gcc t$1.cc
> t$1.cc: In function `int main()':
> t$1.cc:3: call of overloaded `pow' is ambiguous
> c:/djgpp/cplusinc/builtin.h:47: candidates are: pow(long int, long int)
> c:/djgpp/cplusinc/builtin.h:46:                 pow(double, long int)
> c:/djgpp/include/math.h:120:                 pow(double, double)
> t$1.cc:4: call of overloaded `pow' is ambiguous
> c:/djgpp/cplusinc/builtin.h:47: candidates are: pow(long int, long int)
> c:/djgpp/cplusinc/builtin.h:46:                 pow(double, long int)
> c:/djgpp/include/math.h:120:                 pow(double, double)
> 
>   How ever can I get all three of these pow(,) functions current at once?, so
> that any call of pow(,) gets the form that needs the least amount of implicit
> coercions, like I thought was the rule and the purpose of overloading?

This is kind of horrible, but I took a look at the ANSI draft for C++
(which is available now at ftp://research.att.com/dist/stdc++/WP/),
and it turns out that g++ conforms to the standard.  The conversions
that have to be done in your case are

int --> long int

vs.

int --> double

It turns out that under the current rules, these conversions are 
considered equally good, thus the ambiguity.  The resolver doesn't
"know" that "int" and "long int" are identical on our system.

So if you say

long int i;
double x;
pow(x, i);

it works as you expect.  It might be more useful to have pow(int, int)
declared somewhere.

I always thought this sort of thing was a thorn in the side.  Have you
ever tried to write a general "max" template function??  It's not trivial.
I gave up and used the obvious macro.

Gordon

--
Gordon J. Hogenson <ghogenso AT u DOT washington DOT edu>         Tel. (206) 685-2951

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019