0

Section 7.2 Enumeration declarations doesn't say anything about the operator!=() and the operator==() for a scoped enumeration. But the code below compiles.

#include <iostream>

enum class Month{jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov , dec};

int main()
{
    Month a = Month::feb;
    Month b = Month::jul;
    Month c = a;

    if( a != b ) std::cout << "a |= b" << '\n';
    if( a == c ) std::cout << "a == c" << '\n';
}
8
  • Why are you expecting it shouldn't? The standard doesn't say something about these operators for int in particular either. Commented Mar 24, 2014 at 12:30
  • @MikeSeymour §5.10 also doesn't say anything for enum classes. Commented Mar 24, 2014 at 12:32
  • @WakeupBrazil: Indeed, it defers to 5.9. I've extended my comment into an answer, since it's not entirely trivial. Commented Mar 24, 2014 at 12:34
  • @PeterHorvath hence my empiricist answer, such things gave rise to GNUC, and a lot of other things which eventually become standard (example BOOST) Commented Mar 24, 2014 at 12:35
  • 1
    @PeterHorvath I find your description of the committee members quite arrogant. Commented Mar 24, 2014 at 13:12

3 Answers 3

6

The built-in operators are specified in 5.10:

The == (equal to) and the != (not equal to) operators have the same semantic restrictions, conversions, and result type as the relational operators except for their lower precedence and truth-value result.

This defers the specification to that of the relational operators in 5.9; for enumerations that's specified by 5.9/5:

If both operands (after conversions) are of arithmetic or enumeration type, each of the operators shall yield true if the specified relationship is true and false if it is false.

So, as one might expect, the comparison operators are applicable to enumerations, comparing the numeric values.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm accepting your answer with some reluctance. It seems like the Standard is not entirely correct on this point. That's probably the reason why they changed the wording in §5.10 in the drafts for C++14, as shown in Vlad's answer.
@WakeupBrazil The standard is correct, as it validates itself ;)
@WakeupBrazil: I don't know what you mean by "not entirely correct". I agree that a standalone specification (per C++14) is clearer than deferring to another specification; but as far as I can see both are equivalent.
2

From the C++ Standard

5.10 Equality operators [expr.eq] 1 The == (equal to) and the != (not equal to) operators group left-to-right. The operands shall have arithmetic, enumeration, pointer, or pointer to member type, or type std::nullptr_t. The operators == and != both yield true or false, i.e., a result of type bool. In each case below, the operands shall have the same type after the specified conversions have been applied.

As you see enumerations independing of whether they are scoped or unscoped can be used in equality operators

3 Comments

5.10 in the C++11 Standard is not what you posted above.
@Wake up Brazil I posted from the Draft #N3691 date 2013-05-16. It seems it is the Draft of the C++ 2014.:)
@WakeupBrazil: This appears to be from the C++14 draft.
1

The WHOLE POINT of enums is an equality (and not) test. ints have an order, 1<2, but EMPTY WHITE and BLACK (if you haev an Othello board say, it just so happens I was working on one 2 hours ago) is WHITE>BLACK, what about WHITE%EMPTY, no, but the WHOLE POINT is so I can say

switch(board.get_square(x,y)) {
    case Piece::WHITE:
        magic();
        break;
    case Piece::BLACK:
        whatever();
        break;
    default:
 }

For an enum class Piece; of course.

case is like an equality comparison I am saying:

if(board.get_square(x,y) == Piece::WHITE) {

or something.

1 Comment

@DarkCthulhu what dont you get?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.