0

Is there a way to distinguish between an enum and an enum class when passed as a template parameter.

In my class I need to serialize an enum or an enum class to and from an integer/byte array. Now for the normal enum I do not strictly have to static cast the value back and forth. For the enum class of course I do.*

This is a simplified example of what I could use:

tempate<class TYPE>
class X 
{
  public:
    X()
    {
      static_assert(std::is_enum<TYPE>, "Only enum and enum class allowed.");
    }

    TYPE get() 
    {
      // This is the point of interest
      if(std::is_enum_class<TYPE>::value)
      { 
        return static_cast<TYPE>(value);
      }
      else
      {
        return value;
      }
    }

  private:
    int value;
};

I am considering whether there is a way to distinguish between the two along the line of std::is_enum?

  • Is it even worth bothering? It is a high performance implementation but would the cast of an enum not be almost instantaneous?
Bart
  • 1,405
  • 6
  • 32

0 Answers0