Skip to content

Enums cannot be compared in Qt 5 #89

@awilfox

Description

@awilfox

3b1931e added operator< to MetaEnumVariable. However, contrary to the commit message, no call to QMetaType::registerComparators is made.

In Qt 6, this works because it uses introspection to determine if operator< is implemented in the class. However, in Qt 5 the call must be explicit.

Oddly, it seems to work on most platforms anyway, somehow… except 64-bit PowerPC. On this platform, using Qt 5.15 + KDE Patch Collection, and GCC 8.5.0, with -O2, or -O0 -fstack-protector -fschedule-insns -fmove-loop-invariants -finline-functions-called-once -fguess-branch-probability (minimum optimisation flags needed to trigger):

FAIL!  : TestBuiltinSyntax::testEnums(gadget-enums-compare05) Compared values are not the same                                          
   Actual   (result): "true"                                 
   Expected (output): "false"                                                                                                           
   Loc: [/usr/src/packages/user/grantlee/src/grantlee-5.3.1/templates/tests/testbuiltins.cpp(482)]
PASS   : TestBuiltinSyntax::testEnums(gadget-enums-compare06)                                                                           
FAIL!  : TestBuiltinSyntax::testEnums(gadget-enums-compare07) Compared values are not the same
   Actual   (result): "false"                                                                                                           
   Expected (output): "true"                                                                                                            
   Loc: [/usr/src/packages/user/grantlee/src/grantlee-5.3.1/templates/tests/testbuiltins.cpp(482)]                                      

Adding a call to registerComparators in testbuiltins makes it pass:

diff --git a/templates/tests/testbuiltins.cpp b/templates/tests/testbuiltins.cpp
index fe7e4ed..82a065d 100644
--- a/templates/tests/testbuiltins.cpp
+++ b/templates/tests/testbuiltins.cpp
@@ -298,6 +298,7 @@ void TestBuiltinSyntax::testObjects()
   Q_UNUSED(s3);
 
   QMetaType{qMetaTypeId<MetaEnumVariable>()}.create(nullptr);
+  QMetaType::registerComparators<MetaEnumVariable>();
 }
 
 void TestBuiltinSyntax::testTruthiness_data()
    Start 2: testbuiltins
1/1 Test #2: testbuiltins .....................   Passed    0.25 sec

100% tests passed, 0 tests failed out of 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions