Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using numeric_limits::max() in constant expressions

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:

  1. 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?

  2. 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.

like image 531
FireAphis Avatar asked Apr 29 '10 15:04

FireAphis


People also ask

What is Numeric_limits int >:: max ()?

std::numeric_limits::max(): The std::numeric_limits<T>::max() function is used to get the maximum finite value representable by the numeric type T. All arithmetic types are valid for type T. Header File: #include<limits>

What is numeric_ limits in c++?

std::numeric_limits ::digits in C++ with Example The std::numeric_limits ::digits function is used to find the number of radix digits that the data type can represent without loss of precision.

Which of the following data types is accepted by the Numeric_limits function?

Data types that supports std::numeric_limits() in C++ std::numeric_limits<int>::max() gives the maximum possible value we can store in type int. std::numeric_limits<unsigned int>::max()) gives the maximum possible value we can store in type unsigned int.


2 Answers

Looks like a bit of a defect...

In C++0x, numeric_limits will have everything marked with constexpr, meaning you will be able to use min() and max() as compile-time constants.

like image 95
UncleBens Avatar answered Sep 21 '22 08:09

UncleBens


While the current standard lacks support here, for integral types Boost.IntegerTraits gives you the compile time constants const_min and const_max.

The problem arises from §9.4.2/4:

If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression (5.19). In that case, the member can appear in integral constant expressions.

Note that it adds:

The member shall still be defined in a name- space scope if it is used in the program and the namespace scope definition shall not contain an initializer.

As others already mentioned numeric_limits min() and max() simply aren't integral constant expressions, i.e. compile time constants.

like image 45
Georg Fritzsche Avatar answered Sep 21 '22 08:09

Georg Fritzsche