C++ Core Guidelines: Regular and SemiRegular Types


The topic for today is quite important when you create your type: Regular and SemiRegular types.


banner 1183445 1280

Here is the exact rule for today.

T.46: Require template arguments to be at least Regular or SemiRegular

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, which are defined by concepts.


  • DefaultConstructible
  • CopyConstructible, CopyAssignable
  • MoveConstructible, MoveAssignable
  • Destructible
  • Swappable
  • EqualityComparable


  • 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 the well-known rule of six:

  • Default constructor: X()
  • Copy constructor: X(const X&)
  • Copy assignment: operator=(const X&)
  • Move constructor: X(X&&)
  • Move assignment: operator=(X&&)
  • Destructor: ~X()

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.

// semiRegular.cpp

#include <iostream>
#include <type_traits>

int main(){
    std::cout << std::boolalpha << std::endl;
    std::cout << "std::is_default_constructible<int&>::value: " << std::is_default_constructible<int&>::value << std::endl;
    std::cout << "std::is_copy_constructible<int&>::value: " << std::is_copy_constructible<int&>::value << std::endl;
    std::cout << "std::is_copy_assignable<int&>::value: " << std::is_copy_assignable<int&>::value << std::endl;
    std::cout << "std::is_move_constructible<int&>::value: " << std::is_move_constructible<int&>::value << std::endl;
    std::cout << "std::is_move_assignable<int&>::value: " << std::is_move_assignable<int&>::value << std::endl;
    std::cout << "std::is_destructible<int&>::value: " << std::is_destructible<int&>::value << std::endl;
    std::cout << std::endl;
    std::cout << "std::is_swappable<int&>::value: " << std::is_swappable<int&>::value << std::endl;        // requires C++17

    std::cout << std::endl;



First of all. The function std::is_swappable requires C++17. Second here is the output.


You see, a reference such as an int& is not defaulted 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.


In C++20 we might get the detection idiom which is part of the library fundamental TS v2. Now, it's a piece of cake to implement isEqualityComparable.

// equalityComparable.cpp

#include <experimental/type_traits>                                                       // (1)
#include <iostream>

template<typename T>
using equal_comparable_t = decltype(std::declval<T&>() == std::declval<T&>());           // (2)

template<typename T>
struct isEqualityComparable: 
       std::experimental::is_detected<equal_comparable_t, T>{};                           // (3)

struct EqualityComparable { };                                                            // (4)
bool operator == (EqualityComparable const&, EqualityComparable const&) { return true; }

struct NotEqualityComparable { };                                                         // (5)
int main() {
    std::cout << std::boolalpha << std::endl;
    std::cout << "isEqualityComparable<EqualityComparable>::value: " << 
                  isEqualityComparable<EqualityComparable>::value << std::endl;
    std::cout << "isEqualityComparable<NotEqualityComparable>::value: " << 
                  isEqualityComparable<NotEqualityComparable>::value << std::endl;
    std::cout << std::endl;


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 even possible just to define operator <=> as defaulted such as for the type Point.

class Point {
   int x;
   int y;
   auto operator<=>(const Point&) const = default;
// compiler generates all six relational operators


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 the evaluation of 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.

// isRegular.cpp

#include <experimental/type_traits>
#include <iostream>

template<typename T>
using equal_comparable_t = decltype(std::declval<T&>() == std::declval<T&>());

template<typename T>
struct isEqualityComparable: 
       std::experimental::is_detected<equal_comparable_t, T>

template<typename T>
struct isSemiRegular: std::integral_constant<bool,
                                      std::is_default_constructible<T>::value &&
                                      std::is_copy_constructible<T>::value &&
                                      std::is_copy_assignable<T>::value &&
                                      std::is_move_constructible<T>::value &&
                                      std::is_move_assignable<T>::value &&
                                      std::is_destructible<T>::value &&
                                      std::is_swappable<T>::value >{};
template<typename T>
struct isRegular: std::integral_constant<bool, 
                                         isSemiRegular<T>::value &&
                                         isEqualityComparable<T>::value >{};
int main(){
    std::cout << std::boolalpha << std::endl;
    std::cout << "isSemiRegular<int>::value: " << isSemiRegular<int>::value << std::endl;
    std::cout << "isRegular<int>::value: " << isRegular<int>::value << std::endl;
    std::cout << std::endl;
    std::cout << "isSemiRegular<int&>::value: " << isSemiRegular<int&>::value << std::endl;
    std::cout << "isRegular<int&>::value: " << isRegular<int&>::value << std::endl;
    std::cout << std::endl;


The usage of the new type-traits isSemiRegular and isRegular makes the main program quite readable.


What's next?

With my next post, I jump directly to the template definition.




Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Louis St-Amour, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Tobi Heideman, Daniel Hufschläger, Red Trip, Alexander Schwarz, Tornike Porchxidze, Alessandro Pezzato, Evangelos Denaxas, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Dimitrov Tsvetomir, Leo Goodstadt, Eduardo Velasquez, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, and Michael Young.


Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, and Bhushan Ivatury.



My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small



I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

Bookable (Online)


Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.


Contact Me

Modernes C++,







My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Interactive Course: The All-in-One Guide to C++20

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code


Today 2456

Yesterday 7541

Week 25382

Month 194799

All 7462639

Currently are 157 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments