1

Refer to https://stackoverflow.com/a/5094430/391104

//typedef enum {Unknown = -1 Linux=7, Apple=2, Windows=100} OS_type;
DEFINE_ENUM_WITH_STRING_CONVERSIONS(OS_type, (Linux)(Apple)(Windows))

int main()
{
    OS_type t = Windows;
    std::cout << ToString(t) << " " << ToString(Apple) << std::endl;
}

The problems I have are:

1> the OS_type is defined in the library and I don't have permission to change it.

2> In addition, the value of the enum is customized!

What should I do?

Community
  • 1
  • 1
q0987
  • 34,938
  • 69
  • 242
  • 387

1 Answers1

0

Just provide an overload for the stream output operator:

std::ostream& operator <<(std::ostream& dst, const OS_type& ostype)
{
    switch (ostype) {
        case Unknown: dst << "Unknown"; break;
        case Linux:   dst << "Linux"; break;
        case Apple:   dst << "Apple"; break;
        case Windows: dst << "Windows"; break;
        default:      dst << "invalid";
    }
    return dst;
}

You can now just do:

int main()
{
    OS_type ostype = Apple;
    std::cout << ostype << '\n';
}
Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • We have many enums defined in the third party library and it is a painful process to do one at a time. – q0987 Jun 13 '13 at 15:23