5

Given int a;, I know that the following returns the largest value that a can hold.

numeric_limits<int>::max()

However, I'd like to get this same information without knowing that a is an int. I'd like to do something like this:

numeric_limits<typeof<a>>::max()
Not with this exact syntax, but is this even possible using ISO C++?


Thanks, all. Aurélien Vallée's type_of() comes closest, but I'd rather not add anything extra to our codebase. Since we already use Boost, Éric Malenfant's reference to Boost.Typeof led me to use

numeric_limits<BOOST_TYPEOF(m_focusspeed)>::max()

I'd never used it before. Again, thanks for so many well-informed responses.

plong
  • 1,723
  • 3
  • 14
  • 14

5 Answers5

9
template<typename T>
T get_lim( const T & x)
{
 return numeric_limits<T>::max();
}

the good thing is that you can use it without explicitly specifying T:

size_t l = get_lim(34);

alexkr
  • 4,580
  • 1
  • 24
  • 21
  • 2
    of course, this doesn't give you a compile-time constant. In C++0x this could be a `constexpr` and you could get a compile-time constant. – rlbond Oct 16 '09 at 17:08
  • `min` and `max` do not return `size_t`. They should return T (this seems obvious). – NewbiZ Oct 16 '09 at 17:13
  • Snippet from numeric_limits: template class numeric_limits { public: static T min() throw(); static T max() throw(); }; – NewbiZ Oct 16 '09 at 17:14
  • @Aurélien Vallée: you are right, but I can return size_t since I wrote the function. Anyway I have this corrected. Thanx. :) – alexkr Oct 16 '09 at 17:15
  • Slightly more general: template std::numeric_limits NumericLimitsOf(const T&){ return std::numeric_limits(); } – Éric Malenfant Oct 16 '09 at 18:15
7

Just FWIW, C++ 0x will also have decltype, which is nearly the same as typeof. They picked a new name primarily because the semantics are different in one case. The existing implementation of typeof (gcc) drops references from types, so typeof(int &) == int. The standard requires that decltype(int &) == int&. This doesn't matter very often, but they decided to use a different name to prevent any silent changes to existing code.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
6

numeric_limits is what is known as a type trait. It stores information relative to a type, in an unobtrusive way.

Concerning your question, you can just define a template function that will determine the type of the variable for you.

template <typename T>
T valued_max( const T& v )
{
  return numeric_limits<T>::max();
};

template <typename T>
T valued_min( const T& v )
{
  return numeric_limits<T>::min();
};

or just create a small type returning structure:

template <typename T>
struct TypeOf
{
  typedef T type;
};

template <typename T>
TypeOf<T> type_of( const T& v )
{
  return TypeOf<T>();
}

int a;
numeric_limits<type_of(a)::type>::max();
NewbiZ
  • 2,395
  • 2
  • 26
  • 40
  • 1
    Sadly, I don't think that "type_of(a)::type" is legal. If such a simple trick would have worked, Boost.Typeof would have not existed :) – Éric Malenfant Oct 16 '09 at 17:43
  • 1
    @Eric You're right. I just checked and type_of(a)::type cannot appear in a constant expression, such as a template parameter :( @Plong: I found a link to an explanation of how to implement it properly. I checked in boost, and they are using the same trick, see : http://www.peousware.com/implementer-un-typeof-et-un-foreach-en-c/ – NewbiZ Oct 16 '09 at 23:15
1

numeric_limits<typeof(a)> works with GCC. (If you have it in standards-compliant mode, you may need to use __typeof__ instead.)

ephemient
  • 198,619
  • 38
  • 280
  • 391
1

Starting with C++11, you can use decltype():

numeric_limits<decltype(a)>::max()

See also Difference between decltype and typeof?.

Oliver
  • 27,510
  • 9
  • 72
  • 103