My post for today is just loosely coupled to the rules of the C++ core guidelines because they do provide not much content. Inspired by the guidelines, today’s post concerns a generic isSmaller function.
Here are the rules for today. They are about the specialization of templates.
- T.64: Use specialization to provide alternative implementations of class templates
- T.65: Use tag dispatch to provide alternative implementations of functions
- T.67: Use specialization to provide alternative implementations for irregular types
The specialization of function templates is also one of my topics for today.
Comparing two Accounts: The First
Let me start simply. I have a class Account. I want to know if an account is smaller than another account. Smaller means, in this case, that the balance is lower.
To simplify my job, I wrote a generic isSmaller function (1) for comparing two values. As you presumably expected, I can not compare accounts because its operator< is not overloaded.
Before I variously solve this issue, I want to make a short detour to SemiRegular and Regular types. This is for one reason the original definition of Regular from Alexander Stepanov and the definition as a concept in C++20 differs in one critical point: ordering.
SemiRegular and Regular Types
Rule T.67: Use specialization to provide alternative implementations for irregular types talks about irregular types. The informal term irregular types stand for either SemiRegular or Regular. To remind you. Here is the definition of SemiRegular and Regular types.
- CopyConstructible, CopyAssignable
- MoveConstructible, MoveAssignable
- SemiRegular – EqualityComparable
If you want more details about Regular and SemiRegular, read my post C++ Core Guidelines: Regular and SemiRegular Types. The account is a SemiRegular but not a Regular type.
The output of the program shows it.
For the details of the program, read my already mentioned post: C++ Core Guidelines: Regular and SemiRegular Types.
By adding the operator == to Account, Account becomes Regular.
But Account is still not comparable.
This is the crucial difference between Regular types defined by Alexander Stepanov and the concept of Regular, which we get with C++20. Due to Alexander Stepanov, a Regular type should support a total ordering.
Now, I have come back to my original plan.
Comparing Two Accounts: The Second
The key idea for my variations is that instances of Account should support the generic isSmaller function.
Overloading operator <
Overloading operator < is probably the most obvious way. Even the error message to the program isSmaller.cpp showed it.
The output of this and the next program is the same; therefore, I skip it.
Full Specialisation of isSmaller
If you can not change the definition of Account, you can at least entirely specialize isSmaller for Account.
By the way, a non-generic function bool isSmaller(Account fir, Account sec) would also do the job.
Extend isSmaller with a Binary Predicate
There is another way to extend isSmaller. I spend the generic function an additional type parameter Pred, which should hold the binary predicate. This pattern is often used in the standard template library.
The generic function has std::less<T> as the default ordering (1). The binary predicate Pred is instantiated in line (2) and used in line (3). If you don’t specify the binary predicate, std::less is used. Additionally, you can provide your binary predicates, such as in line (4) or line (5). A lambda function is an ideal fit for this job.
Finally, here is the output of the program:
What are the differences between these three techniques?
Comparing two Accounts: The Third
The full specialization is not a general solution because it only works for the function isSmaller. In contrast, the operator < is quite often applicable, and any type can use the predicate. The operator < and the full specialization are static. This means the ordering is defined at compile time and is encoded in the type or the generic function. In contrast, the extension can be invoked with different predicates. This is a runtime decision. The operator < extends the type, the other both variants the function. The extension with a predicate allows it to order your type in various ways. For example, you can compare strings lexicographically or by their length.
Based on this comparison, a good rule of thumb is to implement an operator < for your types and add an extension to your generic functions if necessary. Therefore, your type behaves Regular according to Alexander Stepanov, and supports customized orderings.
The next post is about templates. In particular, it is about template hierarchies.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
|My special thanks to SHAVEDYAKS|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,