The concept of a regular type goes back to the creator of the Standard Template Library (STL) Alexander Stepanov. A regular type is a user-defined type and behaves like a built-in type.
The term concrete type is strongly related to the term regular type. Let me, therefore, start this post with the term concrete type.
- A concrete type is “the simplest kind of a class” according to the C++ Core Guidelines. It is often called a value type and is not part of a type hierarchy.
- A regular type is a type that “behaves like an int” and has, therefore, to support copy and assignment, equality, and order.
Here are more observations from the C++ Core Guidelines:
Use a concrete type if you do not have a use case for a class hierarchy. A concrete type is way easier to implement, smaller, and faster. You do not have to worry about inheritance, virtuality, references, or pointers, including memory allocation and deallocation. There is no virtual dispatch and, therefore, no run-time overhead.
To make a long story short: Apply the KISS principle (keep it simple, stupid). Your type behaves like a value.
Regular types (ints) are easier to understand. They are per se intuitive. This means that if you have a concrete type, consider upgrading it to a regular one. The built-in types, such as
double, are regular but so are the containers such as
Finally, let me write about regular types
First of all, the concept regular types goes back to Alexander Stepanov, the father of the STL. He described his ideas about regular types in his paper “Fundamentals of Generic Programming” and refined it in his book “Elements of Programming“.
What is the benefit of a regular type? Alexander Stepanov gives the answer in his book “Elements of Programming“: There is a set of procedures whose inclusion in the computational basis of a type lets us place objects in data structures and use algorithms to copy objects from one data structure to another. We call types having such a basis regular, since their use guarantees regularity of behavior and, therefore, interoperability.
To make it short, regular types behave intuitively in data structures and algorithms such as built-in types.
What does it mean for a regular type to be intuitive? According to the paper “Fundamentals of Generic Programming“, a regular type should support the following operations:
I assume these operations are familiar to you. In C++20, we have the concept
std::regular in C++20.
std::regular is pretty similar to Stepanovs idea of regular types. It includes essentially move semantics:
In C++20, we refine the idea of regular types into two concepts:
A semiregular type
X has to support the rule of six (see my last post “The Rule of Zero/Six” and has to be swappable.
- Default constructor:
- Copy constructor:
- Copy assignment:
X& operator = (const X&)
- Move constructor:
- Move assignment:
X& operator = (X&&)
Only one property is left, and a semiregular type X becomes regular.
A regular type T is regular and equality comparable:
- Equality operator:
operator == (const X&, const X&)
- Inequality operator:
operator != (const X&, const X&)
std::regular is in C++20 based on the three concepts
Furthermore, the concept
std::movable is based on the concepts
std::assignable_from. All other components of the concepts
std::semiregular are also C++20 concepts.
When you want to know more about concepts, read my post: concepts.
You may wonder: Which type is not regular? The most prominent one is probably a reference.
A reference is not an object and is neither regular nor semiregular.
Let’s try it out:
The output of the program shows is. A reference (line 1) is not an object:
You may wonder about line 2 in the program
referenceIsObject.cpp. A reference
int& is not an object, but a reference wrapper
std::reference_wrapper<int> is one. Here is the description of a reference wrapper on cppreference.com:
std::reference_wrapper is a class template that wraps a reference in a copyable, assignable object. It is frequently used as a mechanism to store references inside standard containers (like std::vector) which cannot normally hold references.
This means that a vector of references
std::vector<int&> is not valid, but a vector of reference wrappers
std::vector<std::reference_wrapper<int>> is. Consequently, since C++11, you can have a container with reference semantics.
Modifying the elements of a
std::vector<std::reference_wrapper<int>> (line 1) also modifies the referenced objects (line 2).
Let’s go one step back. What happens in C++20 when you have an algorithm that requires a regular type but uses a reference?
I compiled the program with the latest GCC 12.2, the latest clang 15.0.0, and the latest MSVC v19 compiler. The Microsoft compiler provided the best error message:
A value object is a small object whose equality is based on the statem but not on its identity. They will be the topic of 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, and Marco Parri Empoli.
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,