谁都没错?
$ cat abs.cpp
include <cstdlib>
include <iostream>
using namespace std;
int main()
{
cout<<abs(-19.50)<<endl;
return 0;
}
编译你会得到如下错误:
abs.cpp: In function ‘int main()’:
abs.cpp:7: error: call of overloaded ‘abs(double)’ is ambiguous
/usr/include/stdlib.h:699: note: candidates are: int abs(int)
/usr/lib/gcc/i386-redhat-linux/4.3.0/../../../../include/c++/4.3.0/cstdlib:175: note: long long int __gnu_cxx::abs(long long int)
/usr/lib/gcc/i386-redhat-linux/4.3.0/../../../../include/c++/4.3.0/cstdlib:144: note: long int std::abs(long int)
为啥呢?因为abs()在<cmath>中,而不是C++标准参考手册中提到的<cstdlib>!
谁错了捏?其实谁都没错。这里解释到:
The Standard C++ library is supposed to
overload abs the way you expect, but it’s common for implementations
to omit the overloads, or put them in the wrong place (<cmath> instead
of <math.h>).
这个真的让人很无语,一边是出错,另一边是明知道出错也这么做。。。冏。。。