When you want to define a concrete type that works well in the C++ ecosystem, you should define a type that “behaves link an int“. Formally, your concrete type should be regular. In this post, I define the concepts Regular and SemiRegular.
Regular and SemiRegular are essential ideas in C++. Sorry, I have to say concepts. For example, the rule T.46 from the C++ Core Guidelines: T.46: Require template arguments to be at least Regular or SemiRegular. Only one important question is left to answer: What are Regular or SemiRegular types? Before I dive into the details, this is the informal answer:
- A regular type “behaves link an int“. It could be copied and the result of the copy operation is independent of the original one and has the same value.
Okay, let me be more formal. A Regular type is also a SemiRegular type. Consequentially I start with a SemiRegular type.
A SemiRegular type has to support the rule of six and be swappable.
- Default constructor: X()
- Copy constructor: X(const X&)
- Copy assignment: operator=(const X&)
- Move constructor: X(X&&)
- Move assignment: operator=(X&&)
- Destructor: ~X()
- swappable: swap(X&, Y&)
This was easy. Thanks to the type-traits library, defining the corresponding concepts is a no-brainer. Let me first define the corresponding type-trait isSemiRegular and then use it to define the concept SemiRegular.
Only one little step, and I’m done with the concept Regular. Additionally, to the concept SemiRegular the concept Regular requires that the type is equally comparable. I already defined in my last post the concept Equal.
Let me reuse the concept Equal to define the concept Regular.
Now, I’m curious. How are SemiRegular and Regular defined in C++20?
The concepts regular and semiregular in C++20
There is no reason to define the concept Regular and SemiRegular but to explain it.
Interestingly, the concept regular is similar to my concept Regular but the concept semiregular is composed of more elementary concepts such as copyable and moveable. The concept movable is based on the function is_object from the type-traits library. From the referenced page, here is a possible implementation of the type-traits is_object.
The final step in my post is missing. Let me try it out.
Usage for the concepts Regular and regular
To simplify it, the function templates behavesLikeAnInt and behavesLikeAnInt2 check if the arguments “behaves like an int“. This means my concept Regular and the C++20 concept regular is used to establish the requirement.
I combined all pieces from the previous code snippets to get the concept Regular (Zeile 1). The functions behavesLikeAnInt (line 2) and behaves behavesLikeAnInt2 (line 3) use both concepts. As the name suggests, the type EqualityComparable (line 4) supports equality but not the type NotEqualityComparable (line 5). Using the type NotEqualityComparable in both functions (lines 6 and 7) is the most interesting part.
If you want to see the program in action, use the link to Compiler Explorer: https://godbolt.org/z/XAJ2w3. The error message in the Compiler Explorer with GCC is very accurate but a little overwhelming. This is probably due to the fact that both concepts failed, concepts are still in an early implementation stage, and the online tools are not as comfortable as a console.
The Concept Regular
Essentially this is the message from my failed concept Regular (line 6) using the Compiler Explorer.
The Concept regular
The C++20 concept regular (line 7) uses a more elaborate implementation. Consequentially, I got a more elaborated error message.
The error message of the window’s compiler is too unspecific.
Now I’m done with my miniseries on concepts in C++20, I’m curious to know your opinion on concepts. Are concepts an evolution or a revolution in C++? I’ll be happy if you drop me an E-Mail, including Thursday (06.02). I will use my next final post to concepts to present your opinions. When I should mention your name, say it explicitly.