The topic for today is quite important when you create your type: Regular and SemiRegular types.
Here is the exact rule for today.
Okay, the first question I have to answer is quite obvious. What is a Regular or a SemiRegular type? My answer is based on the proposal p0898. I assume you may already guess it. Regular and SemiRegular are concepts that are defined by concepts.
- CopyConstructible, CopyAssignable
- MoveConstructible, MoveAssignable
- Regular – EqualityComparable
The term Regular goes back to the father of the Standard Template Library Alexander Stepanov. He introduced the term in his book Fundamentals of Generic Programming. Here is a short excerpt. It’s quite easy to remember the eight concepts used to define a regular type. There is a well-known rule of six:
- Default constructor:
- Copy constructor:
- Copy assignment:
- Move constructor:
- Move assignment:
Just add the Swappable and EqualityComparable concepts to it. There is a more informal way to say that a type T is regular: T behaves like an int.
To get SemiRegular, you have to subtract EqualityComparable from Regular.
I hear your next question: Why should our template arguments at least be Regular or SemiRegular or do as the ints do? The STL containers and algorithms, in particular, assume Regular data types.
What is commonly used but not a Regular type? Right: a reference.
References are not Regular
Thanks to the type-traits library, the following program checks at compile time if int& is a SemiRegular type.
First of all. The function std::is_swappable requires C++17. Second, here is the output.
You see a reference such as int& is not default constructible. The output shows that a reference is not SemiRegular and, therefore, not Regular. To check if a type is Regular at compile-time, I need a function isEqualityComparable which is not part of the type-traits library. Let’s do it by myself.
The new feature is in the experimental namespace (1). Line (3) is the crucial one. It detects if the expression (2) is valid for type T. The type-trait isEqualityComparable works for an EqualityComparable (4) and a NotEqualityComparable (5) type. Only EqualityCompable returns true because I overloaded the Equal-Comparison Operator.
To compile the program, you need a new C++ compiler, such as GCC 8.2.
Until C++20, comparison operators are automatically generated for arithmetic types, enumerations, and with restrictions for pointers. This may change with C++20 due to the spaceship operator: <=>. With C++20, when a class defines operator <=>, automatically the operators ==, !=, <, <=, >, and >= are generated. It’s possible to define operator <=> as defaulted, such as for the type Point.
In this case, the compiler will generate the implementation. The default operator<=> performs a lexicographical comparison on its bases (left-to-right, depth-first) and continues with its non-static member in declaration order. This comparison applies to short-circuit evaluation. This means evaluating a logical expression ends if the result is known.
Now, I have all the ingredients to define Regular and SemiRegular. Here are my new type traits.
The usage of the new type-traits isSemiRegular and isRegular makes the main program quite readable.
With my next post, I jump directly to the template definition.
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, and Bhavith C Achar.
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|
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,