diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2023-07-06 22:45:38 +0200 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2023-07-07 12:20:50 +0200 |
commit | 5560723e9dd8f32a2fb2530b975ba0bb0f9f5ef7 (patch) | |
tree | 6ed7b8951f22e422c480b7bdcf83bdb206c1d024 /src/testlib | |
parent | d22a696b6623716e0b922d833997582a5c415a19 (diff) |
QCOMPARE: restore compatibility with braced-init-lists
a611a9f537f1187825339c2a2214c8ec4a23680f (in Qt 5) added support for
mixed-type comparisons through QCOMPARE. That commit added a new
overload for qCompare taking two types, T1 and T2; but it also left the
same-type qCompare(T, T) overload around, guarded by a Qt 6 version
check.
The mixed-type version is however not a generalization of the same-type
one, because it won't work if one of the arguments doesn't participate
in FTAD. Case in point: braced-init-lists. In Qt 5 this worked:
QCOMPARE(some_container, {42});
but in Qt 6 it does not work any more. The mixed-type overload fails
deduction (can't deduce T2); in Qt 5 the same-type overload deduced
T=SomeContainer, and {42} was used to select a constructor for
SomeContainer.
--
There's a partial, straightforward workaround for this: default T2 to
T1 in the mized-type overload. In that case T2 has a "fallback" if it
cannot be deduced. This is partial because of course doesn't address
the case in which T1 cannot be deduced, but I don't think that is
common at all.
QList is special here, because it has qCompare overloads that makes it
comparable with arrays, initializer lists and more. I don't like that
very much -- we should probably have a qCompare(input_range,
input_range) overload, but that's an exercise for C++20.
Change-Id: I344ba33167829984978cd8d649a1904349a9edab
Pick-to: 6.5 6.6
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Milian Wolff <milian.wolff@kdab.com>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/testlib')
-rw-r--r-- | src/testlib/qtestcase.h | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index 95a1d9160a..7eea2b7b32 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -582,7 +582,7 @@ namespace QTest QTEST_COMPARE_DECL(bool) #endif - template <typename T1, typename T2> + template <typename T1, typename T2 = T1> inline bool qCompare(const T1 &t1, const T2 &t2, const char *actual, const char *expected, const char *file, int line) { |