Also, ich habe mir die Sache nochmals genauer angesehen. ISO 14882:2003 besagt
Dann folgen die Definitionen für diverse Funktionen unter anderem abs() mit diversen Typen. Wichtig ist hierbei zu erwähnen, daß alle Definitionen in <cmath> und <cstdlib> im Namensbereich std erfolgen und nicht im globalen Namensraum. Das kann man unter der Einleitung zur Standard Library lesen
Folgendes Programm reproduziert das Problem unter 10.4 mit g++ 4.0.1
Code:
#include <math.h>
#include <ostream>
#include <iostream>
using namespace std;
int main () {
double x (-5.5);
double y (abs(x)); // hier sollte/wird ISO konform nur int abs(int) aufgerufen
std::cout << "abs(" << x << ") = " << y << "\n";
}
Was zu folgender Fehlermeldung führt
Code:
abs.cc: In function 'int main()':
abs.cc:10: error: call of overloaded 'abs(double&)' is ambiguous
/usr/include/stdlib.h:145: note: candidates are: int abs(int)
/usr/include/c++/4.0.0/cstdlib:169: note: long long int __gnu_cxx::abs(long long int)
/usr/include/c++/4.0.0/cstdlib:130: note: long int std::abs(long int)
Kommentiert man das "using namespace std" aus compiliert der Code und liefert das zu erwartende Ergebnis "int abs(int)" wird benutzt.
Das mathematisch richtige Verhalten erhält man dagegen, wenn man folgendes Programm nimmt.
Code:
#include <cstdlib>
#include <cmath>
#include <ostream>
#include <iostream>
using namespace std;
int main () {
double x (-5.5);
double y (abs(x));
int a (-5);
int b (abs(a));
std::cout << "abs(" << a << ") = " << b << "\n";
std::cout << "abs(" << x << ") = " << y << "\n";
}
Die vorher geposteten Links zu dem Solaris Compiler legen zwar nahe, daß auch in <math.h> und <stdlib.h> die Erweiterungen deklariert werden, aber wie man an der ISO Norm sieht, ist dies wohl nicht korrekt. Warum unter MacOS X das Programm mit einem "using namespace std" nicht mehr compiliert, obwohl ISO 9899 davon nichts besagt ist wunderlich. Ohne dieses Statement compiliert das Programm mit dem von C bekanntem Verhalten. Das mathematisch richtige Verhalten erhält man, wenn man das Programm mit den C++ Headern compiliert. Was auch aus anderen empfehlenswert ist.