Created on 2013-09-04.00:00:00 last changed 102 months ago
Proposed resolution:
See proposed resolution of LWG 2192.
[ 2015-08 Chicago ]
Resolved by 2192
[ 2015-09-11, Telecon ]
Geoff provided combined wording for 2192 after Cologne, Howard to provide updated wording for Kona.
Howard: my notes say I wanted to use is_unsigned instead of 'unsigned integral type'.
Previous resolution from Nicolai [SUPERSEDED]:
Edit [c.math] after p7 as indicated:
-6- In addition to the int versions of certain math functions in <cstdlib>, C++ adds long and long long overloaded versions of these functions, with the same semantics.
-7- The added signatures are:long abs(long); // labs() long long abs(long long); // llabs() ldiv_t div(long, long); // ldiv() lldiv_t div(long long, long long); // lldiv()-?- To avoid ambiguities, C++ also adds the following overloads of abs() to <cstdlib>, with the semantics defined in <cmath>:
float abs(float); double abs(double); long double abs(long double);-?- To avoid ambiguities, C++ also adds the following overloads of abs() to <cmath>, with the semantics defined in <cstdlib>:
int abs(int); long abs(long); long long abs(long long);
[ 2015-03-03, Geoffrey Romer provides improved wording ]
See proposed resolution of LWG 2192.
[ 2014-02-13 Issaquah — Nicolai Josuttis suggest wording ]
[ 2013-09 Chicago ]
This issue is related to LWG 2192
Move to open… and abs(float) and abs(long double). And <cmath> should declare abs(int), abs(long), and abs(long long).
As things currently stand, this program is illegal:#include <cstdlib> int main() { double d = -1.23; double dd = std::abs(d); return 0; }
The call is ambiguous because of the various integer overloads, that's because <cstdlib> provides abs(int) but not abs(double).
This lead one commenter on Stackoverflow to state that abs is dangerous, and to recommend using fabs instead. In general, it makes sense to declare overloaded functions that take user-defined types in the same header as the definition of the user-defined types; it isn't necessary to declare all of the overloads in the same place. But here we're not dealing with any user-defined types; we're dealing with builtin types, which are always defined; all of the overloads should be defined in the same place, to avoid mysterious problems like the one in the code above. The standard library has six overloads for abs:int abs(int); // <cstdlib> long abs(long); // <cstdlib> long long abs(long long); // <cstdlib> float abs(float); // <cmath> double abs(double); // <cmath> long double abs(long double); // <cmath>
These should all be declared in both headers.
I have no opinion on <stdlib.h> and <math.h>.History | |||
---|---|---|---|
Date | User | Action | Args |
2016-08-02 17:19:11 | admin | set | messages: + msg8325 |
2016-08-02 17:19:11 | admin | set | status: open -> resolved |
2015-09-14 22:26:41 | admin | set | messages: + msg7522 |
2015-03-28 18:00:00 | admin | set | messages: + msg7255 |
2014-02-13 05:03:30 | admin | set | messages: + msg6831 |
2014-02-13 05:03:30 | admin | set | messages: + msg6830 |
2014-01-12 12:02:07 | admin | set | messages: + msg6770 |
2014-01-12 12:02:07 | admin | set | status: new -> open |
2013-09-04 00:00:00 | admin | create |