Click here to Skip to main content
15,915,770 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Coder,
I am trying to explore in c++11.but while trying to compile the following snap of code getting lot of error,i know missed the c++ 11 set up.
How to compile the following code.
CSS
int main()
{
    enum class Color
    {
        RED,
        BLUE
    };

    enum class Fruit
    {
        BANANA,
        APPLE
    };

    Color a = Color::RED; // note: RED is not accessible any more, we have to use Color::RED
    Fruit b = Fruit::BANANA; // note: BANANA is not accessible any more, we have to use Fruit::BANANA

    if (a == b) // compile error here, as the compiler doesn't know how to compare different types Color and Fruit
        cout << "a and b are equal" << endl;
    else
        cout << "a and b are not equal" << endl;

    return 0;
}
Posted

A Fruit and a Color aren't the same, that like trying to eat a Yellow in the real world.

enum class is a strongly typed enum - and they are meant to not be comparable unless they are the same type as a result, unlike "standard" enums, which are just syntactic sugar for an integer value.

You can do it, but you'd have to use static_cast to do it - and it's not a good idea!
 
Share this answer
 
Visual Studio 2010 supports only a few C++11 features (see here: https://msdn.microsoft.com/en-us/library/hh567368.aspx[^]).

But your errors are not related to C++11. You can not compare values of different type. You may cast the values to an identical type for comparison. But this should be only done when you know what you are doing:
C++
if (static_cast<int>(a) == static_cast<int>(b))
        cout << "a and b are equal" << endl;
    else
        cout << "a and b are not equal" << endl;
 
Share this answer
 
v2
The whole idea is preventing the developer from comparing variables of these two types and from assigning one to another. Of course, if you are trying to break the intended features, you face certain difficulties. And this is good; this is just one of the measures used to prevent breaking the code. Who wants your "blue apple"? I doubt it would be edible. :-)

However, in rare cases you may want to typecast enumerations. Consider this:
C++
class Fruit {
public:
    enum Values { apple, banana };
};

class ExtendedFruit {
public:
    enum Values {
        apple = Fruit::Values::apple, banana = Fruit::Values::banana,
        pomelo, quince, plum };
};

This way, you can safely typecast between Fruit::Values::apple and ExtendedFruit::Values::apple. The question is: why not having just
C++
enum Fruit { apple, banana };

// The type below would not compile!
enum ExtendedFruit { apple = Fruit::apple, banana = Fruit::banana, pomelo };

Oh, this is the ugliness of C++11, which is a direct consequence of extreme ugliness of C++, the need of backward compatibility with older C++, where the enumeration type values could not be repeated in the same namespace, because they used to be used without those TypeName:: prefixes :-(. Too bad and too late for C++.

—SA
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900