I wrote a few posts about using concepts. Concepts are a named set of requirements. Let’s define a few concepts in this post.
A concept can be defined by a function template or by a variable template. A variable template is new with C++14 and declares a family of variables. If you use a function template for your concept, it’s called a function concept; in the second case a variable concept.
Integral is a variable concept and Equal is a functional concept. Both return a boolean.
- The type parameter T fulfills the variable concept Integral if std::is_integral<T>::value returns true.
- The type parameter T fulfills the function concept Equal if there are overloaded operators == and != for T that returns a boolean.
The function concept Equal look very familiar to me. Why? You will see it in a few sentences. But let me first apply the concept.
I used the concept Equal in the (generic) function areEqual (lines 13 to 15). That’s not so exciting. Here is the output of the function areEqual:
What is more interesting is if I use the class WithoutEqual and WithoutUnequal.I set for both the == or respectively the != operator to delete. The compiler complains immediately that both types do not fulfill the concept.
Equal look familiar to me. Now, you see why.
The concept Equal and Ord
This is part of the type hierarchy of Haskell’s type classes. You have a kind of inheritance between the type classes, denoted by arrows. Looking in the left corner at the top, you will see the typeclass Eq. Now I’m curious how the definition of Eq will look like.
Let’s have a closer look at Haskell’s typeclass Eq. Eq requires, from its instances, that
- they have equal == and inequal /= operation that returns a Bool.
- both take two arguments (a -> a) of the same type.
Of course, the instances are the concrete types such as Int.
Now, I have two questions in mind if I look at Haskell’s type hierarchy. What is the definition of the typeclass Ord in Haskell, and can we model the inheritance relation in C++?
What is the definition of the typeclass Ord in Haskell?
The most exciting point about the typeclass Ord is the first line of its definition. An instance of the typeclass Ord must already be an instance of the typeclass Eq. Ordering is an enumeration having the values EQ, LT, and GT.
How can we model the concept Ord in C++?
Eq -> Ord
Of course, we can define the concept Ord by using all requirements of Eq and Ord. But we can do better in C++:
To simplify my job, I ignored the requirements compare and max in the concept Ord. The critical point about the concept is the line requires Equal<T>(). Here I require that the type parameter T fulfill the requirement Equal. If I use more requirements, such as in the definition of the concept Equal, each requirement from top to bottom will be checked. That will be done in a short-circuiting evaluation. So the first requirement returning false will end the process.
Equality and inequality are defined for the data types int and std::unordered_set. Therefore, the output should not surprise you.
That will change dramatically if I use line 44 because the smaller/bigger operators are not defined for std::unordered_set.
I wrote a few articles for the German Linux-Magazin and iX about C++17. One of my blog readers asked me if they are available in English. I answered no. But I promised him to write about C++17 in my next post.
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,