C++: Print out enum value as text

Viewed 236917

If I have an enum like this:

enum Errors {
    ErrorA = 0,
    ErrorB,
    ErrorC,
};

Then I want to print it out to console:

Errors anError = ErrorA;
std::cout << anError; // 0 will be printed

But what I want is the text "ErrorA". Can I do it without using if/switch? And what is your solution for this?

13 Answers
Related