I would like to define inside a class a constant which value is the maximum possible int. Something like this:
class A
{
...
static const int ERROR_VALUE = std::numeric_limits<int>::max();
...
}
This declaration fails to compile with the following message:
numeric.cpp:8: error: 'std::numeric_limits::max()' cannot appear in a constant-expression numeric.cpp:8: error: a function call cannot appear in a constant-expression
I understand why this doesn't work, but two things look weird to me:
It seems to me a natural decision to use the value in constant expressions. Why did the language designers decide to make max() a function thus not allowing this usage?
The spec claims in 18.2.1 that
For all members declared static const in the numeric_limits template, specializations shall define these values in such a way that they are usable as integral constant expressions.
Doesn't it mean that I should be able to use it in my scenario and doesn't it contradict the error message?
Thank you.
While the current standard lacks support here, for integral types Boost.IntegerTraits gives you the compile time constants
const_min
andconst_max
.The problem arises from §9.4.2/4:
Note that it adds:
As others already mentioned
numeric_limit
smin()
andmax()
simply aren't integral constant expressions, i.e. compile time constants.You want:
Put the class/struct in a header and the definition in a .cpp file.
It doesn't contradict, because
max
is not definedstatic const
. It's just a static member function. Functions can't be const, and static member functions can't have a const attached at the very right either.There is also a
double max()
in the double version of the limits, and in C++03 it wouldn't work to saystatic double const max = ...
. So to be consistent,max()
is a function for all versions of the limit template.Now, it's known that
max()
not being able to be used like that is bad, and C++0x already solves it by making it aconstexpr
function, allowing your proposed usage.1- If you want a static const int in your program to be initialized with a function:
This works on VS 2008
2- If you want to get max and min number for a given data type, then use these definitions INT_MAX, INT_MIN, LONG_MAX and so on..
3- If however you need to use these wrt template type, then hard code the templates yourself
and
and call them like this
4- and if you are only dealing with binary represented types only, then use this:
and this
Hope this can help you..
Looks like a bit of a defect...
In C++0x,
numeric_limits
will have everything marked withconstexpr
, meaning you will be able to usemin()
andmax()
as compile-time constants.