[Development] Heads up: QtAlgorithms to be deprecated in 5.2
Giuseppe D'Angelo
giuseppe.dangelo at kdab.com
Fri Sep 13 22:16:43 CEST 2013
Hello,
https://codereview.qt-project.org/#change,43441 (currently waiting for a
review) is going to mark most of the QtAlgorithms functions as deprecated.
The decision about this has already happened some time ago:
http://www.mail-archive.com/development@qt-project.org/msg01603.html
To summarize it up, there are several good reasons to do this, most notably:
- Qt 5 requires STL;
- STL algorithms are far better than ones in Qt;
- Due to the fact that Qt algorithms use qLess and qSwap, it's just not
possible to reimplement Qt algorithms in terms of the STL counterparts
and keep 100% compatibility with existing code.
=== Impact for user code ===
- A mere search-and-replace will fix most of the deprecation warnings.
Probably the most annoying bit is that Qt offers convenience overloads
taking a container instead of an iterator:
qSort(list); // needs to become: std::sort(list.begin(), list.end())
(If you're reading this and are in the C++ committee, please, you know
what to propose for the next Standard.)
- If you specialized qLess<T> for your type, you need to pass it
*explicitly* to the STL algorithm as the comparison functor:
QList<Foo> list;
std::stable_sort(list.begin(), list.end(), qLess<Foo>());
- If you specialized qSwap<T> for your type, you need to specialize
std::swap<T> to make it called by STL:
namespace std {
template<> void swap(Foo &lhs, Foo &rhs) { ... }
}
(Given you're at it, it's an *extremely good idea* to provide move
operators too. Sorting algorithms will use it in C++11.)
Note that Qt specializes std::swap for you if you use Q_DECLARE_SHARED
for your type, by requiring you to implement a swap() member function.
- Unlike qSort, std::sort requires the comparison functor to really
honour Strict Weak Ordering. This means that if comp(a, b) returns true,
then comp(b, a) *must* return false.
If you start to see crashes inside STL algorithms, please check the
functor. Common mistakes are:
1) functors returning "true" to express "equivalent" items
2) functors using the relative position of two items (in a list/array)
as sorting criterion
Example of 1) https://codereview.qt-project.org/65408
Example of 2) https://codereview.qt-project.org/65015
This also means that if the comparison functor is somehow "user
provided" and you can't trust that it's really enforcing SWO, then you
should stay with Qt algorithms (or maybe write one yourself), as the
Standard says that it's a requirement violation, leading to undefined
behaviour.
- C++03 has a broken wording about algorithms such as lower_bound /
upper_bound: in spite of them having two overloads (one with and one
without a comparison functor), §25.3.3.1.1 [lower.bound] for instance says:
Requires: Type T is LessThanComparable (20.1.2).
I.e. operator<(T, T) must exist. The pitfall is that we're allowed to
pass a value of type V != T as the value to look for, and a functor with
signature
bool f(T, V)
to perform the search. This means that operator<(T, T) might not exist
at all.
Some C++03 compilers (hello, MSVC 2008), when in debug mode, use the
requirement to ease development by checking if the iterator range is
indeed sorted by operator<(T, T). Unfortunately, if the operator doesn't
exist, compilation fails (!). Adding a operator< fixes the issue.
(Probably even a dummy one, which always returns false <disclaimer>I'm
not recommending it</disclaimer>).
https://bugreports.qt-project.org/browse/QTBUG-33473 is tracking this
for qtbase itself.
(The wording has been corrected in C++11, cf. §25.4.3.1.1).
===
Comments?
--
Join us at Qt Developer Days 2013! - https://devdays.kdab.com
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Software Engineer
KDAB (UK) Ltd., a KDAB Group company
Tel. UK +44-1738-450410, Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-independent software solutions
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4048 bytes
Desc: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/development/attachments/20130913/0e0784c3/attachment.bin>
More information about the Development
mailing list