Discussion:
std::abs(int) ambiguous?
(too old to reply)
Helmut Jarausch
2009-03-23 23:17:27 UTC
Permalink
Hi

I cannot find documentation whether

#include <cmath>
using std::abs;

int main() {
int N;
return abs(N);
}

should compile or not.

Currently, gcc-4.3.3 signals an ambiguous overload problem:
absT.C: In function 'int main()':
absT.C:6: error: call of overloaded 'abs(int&)' is ambiguous
/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.3/include/g++-v4/cmath:107: note: candidates are: long double std::abs(long double)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.3/include/g++-v4/cmath:103: note: float std::abs(float)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.3/include/g++-v4/cmath:99: note: double std::abs(double)

Is this a bug in gcc-4.3.3 or is it conforming to the standard.
It the latter, how to use 'abs' for all numeric types?

Thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Bo Persson
2009-03-24 05:01:20 UTC
Permalink
Post by Helmut Jarausch
Hi
I cannot find documentation whether
#include <cmath>
using std::abs;
int main() {
int N;
return abs(N);
}
should compile or not.
absT.C:6: error: call of overloaded 'abs(int&)' is ambiguous
note: candidates are: long double std::abs(long double)
note: float std::abs(float)
note: double std::abs(double)
Is this a bug in gcc-4.3.3 or is it conforming to the standard.
It the latter, how to use 'abs' for all numeric types?
It is a bit odd, but std::abs for integer types are found in
<cstdlib>, not in <cmath>.


Bo Persson
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
litb
2009-03-24 05:02:25 UTC
Permalink
Post by Helmut Jarausch
Hi
[...]
Post by Helmut Jarausch
Is this a bug in gcc-4.3.3 or is it conforming to the standard.
It the latter, how to use 'abs' for all numeric types?
Try including <stdlib> instead.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Neil Butterworth
2009-03-24 05:00:45 UTC
Permalink
Post by Helmut Jarausch
Hi
I cannot find documentation whether
#include <cmath>
using std::abs;
int main() {
int N;
return abs(N);
}
should compile or not.
Not without the ambiguity warnings you noted. The int version of abs()
actually lives in <cstdlib> per section 26.5 of C++ Standard.

Neil Butterworth
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Loading...