如何编写一个最多给出两个args的c ++模板?

ano*_*non 4 c++ templates max

两个参数都保证是整数.

如何编写myMax:

myMax<1, 2>; // 2
myMax<3, 2>; // 3  ?
Run Code Online (Sandbox Code Playgroud)

我希望在编译时评估它,而不是运行时.(然后需要将sizeof用于类型列表以为变量分配空间.)

谢谢!

Jam*_*lis 7

template <int x, int y>
struct myMax
{
    static const int value = (x > y) ? x : y;
};
Run Code Online (Sandbox Code Playgroud)

如果您打算仅使用尺寸,则可以使用std::size_t而不是int.