tags:

views:

116

answers:

1

Both arguments are guaranteed to be integers.

How do I write myMax such that:

myMax<1, 2>; // 2
myMax<3, 2>; // 3  ?

I want this to be evaluated at compile time, not run time. (Need to then use this with sizeof for a typelist to allocate space for a variant.)

Thanks!

+7  A: 
template <int x, int y>
struct myMax
{
    static const int value = (x > y) ? x : y;
};

If you are going to be using it only with sizes, you can use std::size_t instead of int.

James McNellis
You should make it `static`.
Georg Fritzsche
@gf: I know; that was a copy and paste failure. It's been fixed.
James McNellis
I thought you knew, just thought you overlooked.
Georg Fritzsche