⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

QCOMPARE requires explicit casting to handle properly comparison between quint and some number

  • I'm wondering if this is made on purpose or is a bug but the following line

     quint16 num = 0;
     QCOMPARE(num, 0);

    cannot be compiled and the error

    undefined reference to `bool QTest::qCompare<unsigned short, int>(unsigned short const&, int const&, char const*, char const*, char const*, int)'


    However if I do

    QCOMPARE(num, quint16(0));

    everything compiles just fine. I do realize that by forcing the tester to do explicit casting he is forced to check the value that he is actually entering (if we have quint8 for example and compare it to 1000, this clearly introduces a problem) but on the other hand every single time the tester needs to make trivial comparisons he needs to typecast.

    Also the behaviour above is easily avoided by simply replacing QCOMPARE with QVERIFY like so

    QVERIFY((num == 0) == true);

  • Lifetime Qt Champion


    What version of Qt are you using ?
    With which compiler ?
    On what OS ?

Log in to reply