0

I am looking at this Qt's example from the doc http://doc.qt.io/qt-5/statemachine-api.html :

bool eventTest(QEvent *e) override
{
    if (e->type() != QEvent::Type(QEvent::User+1)) // StringEvent
        return false;
    StringEvent *se = static_cast<StringEvent*>(e);
    return (m_value == se->value);
}

But in the line :

e->type() != QEvent::Type(QEvent::User+1)

I do not understand the need for an explicit cast here, that is the integer value to enum Event::Type because the following also works :

e->type() != QEvent::User+1 // no explicit cast    

So is it better to use an explicit cast and why ?

Thank you.

Fryz
  • 2,119
  • 2
  • 25
  • 45
  • 1
    In C++, static_cast is the preferrable way, even if it is an implicit conversion. – P.W Jan 14 '19 at 10:23
  • true, please see this https://stackoverflow.com/questions/11452920/how-to-cast-int-to-enum-in-c – Fabio C. Jan 14 '19 at 10:33
  • i have edited the question to clarify it : actually i am asking whether or not i need an explicit cast here. – Fryz Jan 14 '19 at 11:15

2 Answers2

1

The type of QEvent::User + 1 is int. Casting that result to QEvent::Type turns it into QEvent::Type, of course, and I'm guessing that that's its original type, as well as the type of e->Type().

You're right that the cast isn't needed, although the reason is a bit less direct. In e->Type() == QEvent::User + 1, the right-hand side has type int, so the left-hand side is promoted to int and the comparison is done on the int values. Ordinarily, that's perfectly okay; that's how enums have been used since time immemorial. That cast looks to me like elevating formalism over reality. Some people like to do that.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
0

From the online documentation on enum

There are no implicit conversions from the values of a scoped enumerator to integral types, although static_cast may be used to obtain the numeric value of the enumerator.

So, yes, you need an explicit cast for such conversions.

But even if QEvent::Type is not a scoped enum and allows for an implicit conversion, static_cast is a good way to do it.

Please read this FAQ: What good is static_cast?

P.W
  • 26,289
  • 6
  • 39
  • 76
  • 1
    But QEvent::Type is not a scoped enumeration and it's also working without the explicit cast – Fryz Jan 14 '19 at 11:25
  • I am not familar with Qt and I assumed it was a scoped enum becaues of the `::`. – P.W Jan 14 '19 at 11:27
  • Ok but `e->type()` already returns a `QEvent::Type` enum value and we are comparing it also to a valid value of the enum. – Fryz Jan 14 '19 at 11:44
  • Are `QEvent::Type`, `QEvent::User` and `e->type()` of the same type? – P.W Jan 14 '19 at 11:51
  • `QEvent::Type` and `e->Type()` yes whereas `QEvent::User` is one of the possible value of the `QEvent::Type` enum that is defined in the `QEvent` class – Fryz Jan 14 '19 at 13:18
  • This info was not present in the question. Now it is clear. No need for casting. – P.W Jan 14 '19 at 13:20
  • Indeed i have assumed some Qt's background but the info is here http://doc.qt.io/qt-5/qevent.html – Fryz Jan 14 '19 at 13:33