In C, when you need to check for the limits of native numeric types, such as int or unsigned long, you include the limits.h header file and then use the INT_MIN/INT_MAX and ULONG_MAX macros respectively. In the C++ world, there is a corresponding climits header file to get the definition of these macros, so I always thought this was the way to follow.
However, it turns out that the C++ standard defines a limits header file too, which provides the numeric_limits<T> template. This template class is specialized in
As an example, this C code:
#include <limits.h>becomes the following in C++:
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
printf("Integer range: %d to %dn", INT_MIN, INT_MAX);
return EXIT_SUCCESS;
}
#include <cstdlib>Check out the documentation for more details on additional methods!
#include <iostream>
#include <limits>
int
main(void)
{
std::cout << "Integer range: "
<< std::numeric_limits< int >::min()
<< " to "
<< std::numeric_limits< int >::max()
<< "n";
return EXIT_SUCCESS;
}