summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorOlivier Goffart <ogoffart@woboq.com>2018-04-06 11:55:48 +0200
committerOlivier Goffart (Woboq GmbH) <ogoffart@woboq.com>2018-04-08 14:24:27 +0000
commite759d38d491d9044a0558b1d45911e3b4115e772 (patch)
tree6d5603b274426fe92a414cd366b4e4a92b84fb53 /tests
parent488c4932366dcba8f6acf56091bd74ff04c5695b (diff)
Declare the operator| in the Qt namespace for QFlags in that namespaces
This is a long overdue change so we don't break ADL of operator|. I think will not break source or binary compatibility. The problem is code like this: namespace Foo { struct MyStruct; MyStruct operator|(MyStruct, MyStruct); void someFunction() { fooLabel->setAlignement(Qt::AlignLeft | Qt::AlignTop) } } This would be an error before as ADL would find only the Foo::operator| and not the global one since the arguments are not in the global namespace. After this change, ADL works fine and this code compiles This bites people with misterious error, see questions on https://stackoverflow.com/questions/10755058/qflags-enum-type-conversion-fails-all-of-a-sudden https://stackoverflow.com/questions/39919142/broken-bitwise-or-operator-in-a-qt-project [ChangeLog][QtCore] QFlags's operator| for enum types in the Qt namespace are now declared in the Qt namespace itself. Change-Id: I021bce11ec1521b4d8795a2cf3084a0be1960804 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/global/qflags/tst_qflags.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/tests/auto/corelib/global/qflags/tst_qflags.cpp b/tests/auto/corelib/global/qflags/tst_qflags.cpp
index 2f1b56629a..6129184738 100644
--- a/tests/auto/corelib/global/qflags/tst_qflags.cpp
+++ b/tests/auto/corelib/global/qflags/tst_qflags.cpp
@@ -39,6 +39,7 @@ private slots:
void classEnum();
void initializerLists();
void testSetFlags();
+ void adl();
};
void tst_QFlags::testFlag() const
@@ -304,6 +305,27 @@ void tst_QFlags::testSetFlags()
QVERIFY(!flags.testFlag(MyStrictEnum::StrictFour));
}
+namespace SomeNS {
+enum Foo { Foo_A = 1 << 0, Foo_B = 1 << 1, Foo_C = 1 << 2 };
+
+Q_DECLARE_FLAGS(Foos, Foo)
+Q_DECLARE_OPERATORS_FOR_FLAGS(Foos);
+
+Qt::Alignment alignment()
+{
+ // Checks that the operator| works, despite there is another operator| in this namespace.
+ return Qt::AlignLeft | Qt::AlignTop;
+}
+}
+
+void tst_QFlags::adl()
+{
+ SomeNS::Foos fl = SomeNS::Foo_B | SomeNS::Foo_C;
+ QVERIFY(fl & SomeNS::Foo_B);
+ QVERIFY(!(fl & SomeNS::Foo_A));
+ QCOMPARE(SomeNS::alignment(), Qt::AlignLeft | Qt::AlignTop);
+}
+
// (statically) check QTypeInfo for QFlags instantiations:
enum MyEnum { Zero, One, Two, Four=4 };
Q_DECLARE_FLAGS( MyFlags, MyEnum )