Enum convert to string using compile time constants
01:40 01 Apr 2019

I'm trying to associate compile time strings to enum values.

Here is my first attempt at the problem:

EnumValue will do the compile time assocation between a string and an enum

template
class EnumValue
{
public:
    static const char* toString()
    {
        return EnumStrValue;
    }

    static const int toInt()
    {
        return EnumIntValue;
    }

    static EnumType get()
    {
        return static_cast(EnumIntValue);
    }
};

EnumValueHolder will hold the actual values for both string and enum. I dislike my current design as it still needs to hold a pointer to string. I would prefer a compile time association for this but fail to come up with a more elegant solution

template
class EnumValueHolder
{
public:
    EnumValueHolder()
    {}

    EnumValueHolder(const EnumType& value, const char* str)
        : value(value), str(str)
    {}

    bool operator==(const EnumValueHolder& rhs) { return value == rhs.value; }
    bool operator==(const EnumType& rhs)const { return value == rhs; }

    operator EnumType()const
    {
        return value;
    }

    const char* toString()const
    {
        return str;
    }

    const int toInt()const
    {
        return static_cast(value);
    }

private:
    EnumType value;
    char const* str;
};

Marcos to easily refer to enum types and enum value holder construction

#define ENUM_VALUE_TYPE(enumName, enumValue) \
EnumValue


#define ENUM_VALUE_MAKE(enumName, enumValue) \
EnumValueHolder { \
    ENUM_VALUE_TYPE(enumName, enumValue)::get(), \
    ENUM_VALUE_TYPE(enumName, enumValue)::toString() }

The following are my test cases and usage examples:

const char str_Apple[] = "Apple";
const char str_Orange[] = "Orange";
const char str_Pineapple[] = "Pineapple";


enum class EFruits
{
    Apple,
    Orange,
    Pineapple
};


int main()
{
    auto evApple = ENUM_VALUE_MAKE(EFruits, Apple);
    std::cout << evApple.toString() << std::endl;

    auto evOrange = ENUM_VALUE_MAKE(EFruits, Orange);
    std::cout << evOrange.toString() << std::endl;

    std::cout << "compare: " << (evApple == evOrange) << std::endl;

    evApple = evOrange;
    std::cout << evApple.toString() << std::endl;

    auto myfruit = ENUM_VALUE_MAKE(EFruits, Pineapple);
    std::cout << myfruit.toString() << std::endl;

    switch (myfruit)
    {
    case EFruits::Apple:
        std::cout << "Im an apple!" << std::endl;
        break;

    case EFruits::Orange:
        std::cout << "Im an Orange!" << std::endl;
        break;

    case EFruits::Pineapple:
        std::cout << "Im a Pineapple!" << std::endl;
        break;

    default:break;
    }
}

One of the objectives is to remove the global string:

const char str_Apple[] = "Apple";
const char str_Orange[] = "Orange";
const char str_Pineapple[] = "Pineapple";

The other is to create a macro that assoicates an enum with a string

//Some crazy define that makes pairs of enum values and strings as
//compile time constants
#define DEFINE_ENUM_STRING(enumValue)\
enumValue, #enumValue

//Ideally, the macro would be used like this. This should be usable in any
//scope (global, namespace, class) 
//with any access specifier (private, protected, public)
enum class EFruits
{
    DEFINE_ENUM_STRING(Apple),
    DEFINE_ENUM_STRING(Orange),
    DEFINE_ENUM_STRING(Pineapple)
};

So there are 2 main questions:

1) Will this current design actually guarantee compile time constants for associating the enum to the string?

2) How can I define a macro to stringify an enum value and declare the value in a enum class using 1 line?

Edit: This should work and compile with msvs2017 on win64 platform using c++ 11.

Thanks.

c++ templates enums compile-time-constant