Sign up for the KDAB Newsletter
Stay on top of the latest news, publications, events and more.
Go to Sign-up
Find what you need - explore our website and developer resources
11 May 2022
In this episode Jesper goes on a quest to find the smoothest way to implement operator <() for custom classes.
It takes him via std::tie(), std::tuple() and ends in a discussion comparing QString's taking into account how humans interpret them.
Links:
* More details on comparisons in C++20: https://en.cppreference.com/w/cpp/language/default_comparisons
* why are the operators friends? https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c86-make--symmetric-with-respect-to-operand-types-and-noexcept
The example code showcased in this video is available here: https://github.com/KDAB/kdabtv/tree/master/Qt-Widgets-and-more/lessThan
0:00 operator <() how hard can it be?!
0:25 A naive implementation
1:22 Step 1 - the code for the naive implementation
2:26 hidden friends implementation
3:07 Step 2 - an improved implementation
4:01 Step 3 - using std::tie
5:46 Step 4 - PartialDate (which can be null) - my original implementation
7:00 Step 5 - PartialDate with std::tie
8:31 Step 6 - PartialDate (isNull instead of isValid)
11:09 Step 7 - opertator <=>
13:51 Step 8 - QString comparisons
15:59 localAwareCompare
16:36 specifying sorting method as third parameter to std::sort
All QML tips and tricks videos : https://www.youtube.com/playlist?list=PL6CJYn40gN6jWHP5krsQrVGyYtKh3A3be
All Qt Widgets and more videos:
https://www.youtube.com/playlist?list=PL6CJYn40gN6gf-G-o6syFwGrtq3kItEqI
Stay on top of the latest news, publications, events and more.
Go to Sign-up
Learn Modern C++
Our hands-on Modern C++ training courses are designed to quickly familiarize newcomers with the language. They also update professional C++ developers on the latest changes in the language and standard library introduced in recent C++ editions.
Learn more