This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Problems with std::numeric_limits
- From: Roberto Bagnara <bagnara at cs dot unipr dot it>
- To: libstdc++ at gcc dot gnu dot org
- Cc: The Parma Polyhedra Library developers' list <ppl-devel at cs dot unipr dot it>
- Date: Fri, 13 Jan 2006 19:35:31 +0100
- Subject: Problems with std::numeric_limits
Hi there,
we have been repeatedly bitten by std::numeric_limits because of its
default implementation, i.e., the one beginning with
template<typename _Tp>
struct numeric_limits : public __numeric_limits_base
{
/** The minimum finite value, or for floating types with
denormalization, the minimum positive normalized value. */
static _Tp min() throw() { return static_cast<_Tp>(0); }
/** The maximum finite value. */
static _Tp max() throw() { return static_cast<_Tp>(0); }
/** The @e machine @e epsilon: the difference between 1 and the least
value greater than 1 that is representable. */
static _Tp epsilon() throw() { return static_cast<_Tp>(0); }
...
in the <limits> header file (I am looking at GCC version 4.0.2).
Is that default implementation mandated by the standard?
If so, well... fine. But if it is not mandated then I think it makes
much sense to remove it as it seems a perfect recipe for disaster.
Another issue is: shouldn't, e.g., std::numeric_limits<int> and
std::numeric_limits<const int> be completely equivalent?
Here is a little C++ program demonstrating what I mean:
#include <limits>
#include <iostream>
using namespace std;
struct Foo {
Foo(int) {
};
operator int() {
return 27;
}
};
int main() {
std::cout << std::numeric_limits<Foo>::is_bounded << std::endl;
std::cout << std::numeric_limits<Foo>::max() << std::endl;
std::cout << std::numeric_limits<const int>::is_bounded << std::endl;
std::cout << std::numeric_limits<const int>::max() << std::endl;
std::cout << std::numeric_limits<int>::is_bounded << std::endl;
std::cout << std::numeric_limits<int>::max() << std::endl;
}
Here it prints the following:
0
27
0
0
1
2147483647
All the best,
Roberto
--
Prof. Roberto Bagnara
Computer Science Group
Department of Mathematics, University of Parma, Italy
http://www.cs.unipr.it/~bagnara/
mailto:bagnara@cs.unipr.it