【发布时间】:2012-02-14 08:55:38
【问题描述】:
使用 4.2.1 g++ 编译器出现以下错误:
functions.cpp:24: error: call of overloaded ‘pow(long int&, long int&)’ is ambiguous
/usr/include/architecture/i386/math.h:343: note: candidates are: double pow(double, double)
/usr/include/c++/4.2.1/cmath:373: note: long double std::pow(long double, int)
/usr/include/c++/4.2.1/cmath:369: note: float std::pow(float, int)
/usr/include/c++/4.2.1/cmath:365: note: double std::pow(double, int)
/usr/include/c++/4.2.1/cmath:361: note: long double std::pow(long double, long double)
/usr/include/c++/4.2.1/cmath:357: note: float std::pow(float, float)
这是负责的代码:
long power(long a, long b) {
if (b < 0) return 0;
return pow(a,b);
}
但是,在我的 4.6.1 版本中,我使用两个 longs 进行 pow 评估的代码没有错误或警告(即使使用 -Wextra 标志)。
为什么是这样?对pow 函数使用两个long 是我的错误吗?
【问题讨论】:
-
看起来像 4.6 添加了
pow(long,long)作为扩展 -
标准说应该是double pubs.opengroup.org/onlinepubs/9699919799/functions/pow.html所以我相信只要坚持标准就可以了,只要做个演员表就好了。
-
Anycorn,您能否提供参考(将其作为答案发布)
-
我不能——标准没有提到
pow(long,long)——因此我认为它是一个扩展。 -
您的工作做得很好,指出了编译中的差异!
标签: c++