Max and min values in a C++ enum

后端 未结 6 987
广开言路
广开言路 2020-12-09 00:34

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

6条回答
  •  情歌与酒
    2020-12-09 01:20

    No. An enum in C or C++ is simply a list of constants. There is no higher structure that would hold such information.

    Usually when I need this kind of information I include in the enum a max and min value something like this:

    enum {
      eAaa = 1,
      eBbb,
      eCccc,
      eMin = eAaaa,
      eMax = eCccc
    }
    

    See this web page for some examples of how this can be useful: Stupid Enum Tricks

提交回复
热议问题