vendredi 17 avril 2020

How to implement scoped enum in C++98 and can be used like enum class in C++11?

In C++11, we have scoped enum, and we can using it as follows.

#include <iostream>
enum class Color
{
    RED,
    BLUE,
};
int main()
{
    Color color = Color::RED;
    if (color == Color::RED)
    {
        std::cout << "red" << std::endl;
    }
    return 0;
}

I have already using the scoped enum everywhere in my project.

Now I must move to C++98, so scoped enum can not be used anymore.

How can I implement a scoped enum in C++98 and using just like the one in C++11?

If the implement technique is compilicate, can we extract it into template?

Follow link have already talk about some technique, but not as simple as C++11.

How to use enums in C++

For example:

namespace Color
{
enum MyColor
{
    RED,
    BLUE,
};
}

Thanks for your time.

Aucun commentaire:

Enregistrer un commentaire