
数学的演算を行う関数は通常cmathヘッダをインクロードすればよい。例外はint型abs関数である。この関数はcstdlibの中で定義されている。そのため、以下のようなコードはエラーを返す。
#include <cmath>
using namespace std;
int main(void)
{
int a=10;
return(abs(a));
}
iccでは、
$ icpc cmath.cpp
cmath.cpp(6): error: more than one instance of overloaded function "abs" matches the argument list:
function "std::abs(long double)"
function "std::abs(float)"
function "std::abs(double)"
argument types are: (int)
return(abs(a));
^
gccでは、
$ g++ cmath.cpp cmath.cpp: In function 'int main()': cmath.cpp:6: error: call of overloaded 'abs(int&)' is ambiguous /usr/include/c++/4.4/cmath:94: note: candidates are: double std::abs(double) /usr/include/c++/4.4/cmath:98: note: float std::abs(float) /usr/include/c++/4.4/cmath:102: note: long double std::abs(long double)
下記のコードはエラーなしでコンパイルできる。
#include <cstdlib>
using namespace std;
int main(void)
{
int a=10;
return(abs(a));
}