How to write a C ++ template that gives a maximum of two arguments?

Both arguments are guaranteed to be integers.

How to write myMax so that:

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

I want this to be evaluated at compile time, and not at runtime. (You must then use this with sizeof for the type list to make room for the variant.)

Thanks!

+4
source share
1 answer
 template <int x, int y> struct myMax { static const int value = (x > y) ? x : y; }; 

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

+7
source

Source: https://habr.com/ru/post/1299598/


All Articles