qBound on Arm

When using qBound in combination with qreal, it is important to properly cast the upper and lower bounds, e.g.:

qreal x = qBound(qreal(0.2), v, qreal(1.5));

If you forget this, i.e by using the following code:

qreal x = qBound(0.2, v, 1.5);

Your code will happily work on x86, and some other platforms, but not on Arm. This is because qreal is a double on all other platforms than Arm. On Arm, it is a float. This leads to GCC complaining about not being able to instantiate the template.

10 thoughts on “qBound on Arm”

  1. Personally, I think qreal x = qBound<qreal>(0.2, v, 1.5); is less typing, more readable, and more explicit.

  2. Or, of course, ARM could simply add the proper overloads, but why make life easier for others, right ?

    And, you know, I think I remember having already seen a blogpost about this on planetkde. Twice at least, if memory serves me well.

  3. @Lubos, it would not really be ARM doing the overloading, rather the Qt folks adding a specialized implementation of the function. However, the template only takes one type, and the automagical instantiation reads two (double and qreal, a.k.a. float).

  4. I of course didn’t expect a processor to add a Qt overload. And it’s not necessarily Qt that has to add it, it can be added pretty much anywhere where it fits. As for your ‘however’:

    #ifdef ARM
    inline double qBound( double low, double value, qreal high )
    {
    return qBound( low ,value, high );
    }
    etc. for all 8 combinations … Tadaaa.
    And yes, the return type should be double, given it’s the larger type.

  5. @Lubos I completely understand that one needs to add specializations, I just wanted to clarify that it is not a problem with ARM, per-se, but something that the Qt engineers would have to do.

Comments are closed.