C++ 枚举中的最大值和最小值

2021-12-29 00:00:00 enums c++

有没有办法在 C++ 中找到枚举的最大值和最小值?

Is there a way to find the maximum and minimum defined values of an enum in c++?

推荐答案

不,没有办法在 C++ 中找到任何枚举的最大值和最小值.当需要此类信息时,定义 Last 和 First 值通常是一种很好的做法.例如,

No, there is no way to find the maximum and minimum defined values of any enum in C++. When this kind of information is needed, it is often good practice to define a Last and First value. For example,

enum MyPretendEnum
{
   Apples,
   Oranges,
   Pears,
   Bananas,
   First = Apples,
   Last = Bananas
};

不需要为 FirstLast 之间的每个值指定命名值.

There do not need to be named values for every value between First and Last.

相关文章