2

Possible Duplicate:
Why is std::numeric_limits<T>::max() a function?

I was wondering if someone could explain the reasoning behind why std::numeric_limit<T>::min and max are functions and not constants?

Furthermore, I'd like to know what techniques can be used to make use of the min/max values as part of template parameters, eg:

template<unsigned long long max>
class foo
{
public:
   void boo() { std::cout << max << std::endl; }
};

.
.
.

foo<std::numeric_limits<int>::max()> f;
f.boo();
Community
  • 1
  • 1
Soda Coader
  • 101
  • 12
  • ...and regarding the second part of your question, about instantiating templates, see this answer: http://stackoverflow.com/questions/2738435/using-numeric-limitsmax-in-constant-expressions/2738576#2738576 – John Zwinck May 16 '11 at 00:36
  • @John: Thanks for those answers, but i was hoping for a solution not based on Boost as I don't have access to it in the project I'm currently working on. – Soda Coader May 16 '11 at 00:46
  • Well then you may want to use Aaron's solution below. – John Zwinck May 16 '11 at 01:21

1 Answers1

5

Fall back on good old C!

foo< INT_MAX > f;

or even

const int my_int_max = INTMAX;
foo< my_int_max > f;

Works for me on g++ (Debian 4.4.5-8) 4.4.5

Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88