Is strongly typed enums comparable?

Q

Qiang Li

Hi,

There is no implicit conversion from a scoped enum to int, does this
mean that it is not comparable? E.g.,

enum class X { zero, one };
X::zero < X::eek:ne; // Is this OK?
 
N

newbarker

Hi,

There is no implicit conversion from a scoped enum to int, does this
mean that it is not comparable? E.g.,

enum class X { zero, one };

That's not valid C++ is it? What is a "scoped enum"?

Regards,

Pete
 
N

newbarker

Not yet. I forgot to state that this is a C++0x feature.

OK, my apologies then - I really need to read up about the new
standard. Hope someone comes along in a bit and gives you a better
response!

Pete
 
B

Bo Persson

Qiang said:
Hi,

There is no implicit conversion from a scoped enum to int, does this
mean that it is not comparable? E.g.,

enum class X { zero, one };
X::zero < X::eek:ne; // Is this OK?

Yes, they will be comparable to objects of the same type, but not to
others.


Bo Persson
 
G

Gianni Mariani

Qiang said:
Hi,

There is no implicit conversion from a scoped enum to int, does this
mean that it is not comparable? E.g.,

enum class X { zero, one };
X::zero < X::eek:ne; // Is this OK?

enums support comparisons. Check out the standard. It goes somthing
like order is order of declaration or of the numbering specifically
declared.

There are very few places I would use enums tho. I'd usually use a
class and some suitably defined static objects and place any "enum"
specific behaviour in them. This makes code much easier to extend. Not
to say enums are not useful in some cases tho..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top