tags:

views:

175

answers:

2

How can I get an equivalent of java.lang.Integer.MIN_VALUE on C++?

+18  A: 
#include <limits>    
std::numeric_limits<int>::min();
CTT
That's the easiest way.
Opera
@Opera: And the correct way too. :)
jalf
+3  A: 

Depends what you mean by "equivalent". java.lang.Integer.MIN_VALUE is a compile-time constant in Java, but std::numeric_limits<int>::min() is not an integer constant expression in C++. So it can't be used for example as an array size (well, the min value of an int can't anyway because it's negative, but the same goes for expressions involving it, or other similar values, or other contexts requiring an i.c.e).

If you need a compile-time constant in C++, use INT_MIN from <climits>. In fact you may as well use it anyway: numeric_limits is essential if you're writing generic code, and you have some integer type T which might be int, or might be something else. Its primary use otherwise is to prove your leet C++ skillz, and/or make your code longer ;-)

Steve Jessop
Though in C++0x it's a constant-expression. :)
GMan
I'll believe in C++0x when I put my back out lifting it ;-)
Steve Jessop