Is it possible to determine the number of elements of a c++ enum class?

后端 未结 13 1672
长情又很酷
长情又很酷 2020-12-13 01:39

Is it possible to determine the cardinality of a c++ enum class:

enum class Example { A, B, C, D, E };

I tried to use si

相关标签:
13条回答
  • 2020-12-13 02:14
    enum class TEST
    {
        BEGIN = __LINE__
        , ONE
        , TWO
        , NUMBER = __LINE__ - BEGIN - 1
    };
    
    auto const TEST_SIZE = TEST::NUMBER;
    
    // or this might be better 
    constexpr int COUNTER(int val, int )
    {
      return val;
    }
    
    constexpr int E_START{__COUNTER__};
    enum class E
    {
        ONE = COUNTER(90, __COUNTER__)  , TWO = COUNTER(1990, __COUNTER__)
    };
    template<typename T>
    constexpr T E_SIZE = __COUNTER__ - E_START - 1;
    
    0 讨论(0)
提交回复
热议问题