tags:

views:

466

answers:

3
+2  Q: 

C++: max integer

Is there a C++ cross-platform library that provides me with a portable maximum integer number?

I want to declare:

const int MAX_NUM = /* call some library here */;

I use MSVC 2008 unmanaged.

+6  A: 

See limits.h (C) or climits (C++). In this case you would want the INT_MAX constant.

Lukáš Lalinský
+28  A: 

In the C++ standard library header <limits>, you will find:

std::numeric_limits<int>::max()

Which will tell you the maximum value that can be stored in a variable of type int. numeric_limits is a class template, and you can pass it any of the numeric types to get the maximum value that they can hold.

The numeric_limits class template has a lot of other information about numeric types as well.

James McNellis
Actually it's called a "function template", not the other way around. But otheriwse that's very good advice. +1
sbi
@sbi: Thank you for the correction. The C++ standard does indeed call them "function templates" (14.8.1), but also uses the phrase "nontemplate function," which makes me think that the phrase "template function" might also be acceptable; even highly reputable C++ references (http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13) use the phrase "template function." In any case, I've updated the answer with the unambiguously correct terminology. :-)
James McNellis
Comeau has a faq entry on that: http://www.comeaucomputing.com/techtalk/templates/#terms
Georg Fritzsche
@gf: Thanks; I haven't seen that FAQ before (I've not used Comeau), but there is a lot of interesting stuff there. I have had to correct my answer again, though, since there aren't any template functions or function templates in it. `numeric_limits` is a class template.
James McNellis
In a similar vein, talking about word-order, this "portably provides a maximum integer", it doesn't "provide a portable maximum integer". There does not exist an integer which is a portable maximum integer, for precisely the reason `numeric_limits` exists, which is that it's different on different implementations ;-)
Steve Jessop
A: 

I know the answer has been given but I just want to know from my old days, I used to do

int max = (unsigned int)-1

Will it give the same as

std::numeric_limits<int>::max()

?

Mike Gleason jr Couturier
This trick only works for unsigned max values.
AndreyT
It was a question about the question, why the -2!?
Mike Gleason jr Couturier
For future reference, StackOverflow works best as a 1 Question -> Many Answer type of thing. If you have a question (and the one you bring up seems reasonable), feel free to post a brand new question. Here, it looks like you're trying to offer advice that others perseive as incorrect.
Outlaw Programmer
To expand on what Andrey says: no, it won't give the same. `(int)(unsigned int)-1` is -1 (well, actually it's implementation-defined, but it's usually -1), whereas `std::numeric_limits<int>::max()` is a large positive `int` value. However, `(unsigned int)-1` is equal to `std::numeric_limits<unsigned int>::max()`, always.
Steve Jessop
Hi, yes for the comments!
Mike Gleason jr Couturier