Boost logo

Boost :

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


Hello!

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
{
public:
        Enum() : m_id(0), m_text(NULL) {}
protected:
        Enum(int id, const char* enumText) : m_id(id), m_text
(enumText) {}
        std::string Text() const { return m_text; }
private:
        int m_id;
        const char* m_text;
};

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

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

        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.

/Michel


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk