Boost logo

Boost :

From: Michel André (michel.andre_at_[hidden])
Date: 2000-11-09 04:53:17


Several times I have enountered situations
where I want to convert enums to string.

I have tried to find a solution to the problem
where I can declare the enum value, symbolic
constant and string in on and only on place.

I have used the approach with static but then
i have to declare the enums in the header file
and in an compilation unit.

I have tried this approach with templates but
there seems to be an error when using const char* as
template parameter. Also the myenum constructor fails
to compile (BTW I use MSVC).

class Enum
        Enum() : m_id(0), m_text(NULL) {}
        Enum(int id, const char* enumText) : m_id(id), m_text
(enumText) {}
        std::string Text() const { return m_text; }
        int m_id;
        const char* m_text;

template <int value, char* enumText>
class EnumVal : public Enum
        EnumVal() : Enum(value, enumText) {}

struct MyEnum : public Enum
        //template <int value, char* enumText>
        template <int value, char* enumText>
                MyEnum( const Enum<value,enumText>& val ) : Enum

        typedef EnumVal<1,"MyEnum1"> MyEnum1;

So my question to you Is it possible to use this approach or
is ther a better solution to the general problem.


Boost list run by bdawes at, gregod at, cpdaniel at, john at