Let me continue with the rules for defining concepts in the guidelines. The first of the three remaining rules in this post is quite sophisticated.
Here are the rules for today:
- T.24: Use tag classes or traits to differentiate concepts that differ only in semantics
- T.25: Avoid complimentary constraints
The explanation of the first rules is relatively concise. Maybe, too concise.
This is the reason for this rule from the guidelines: “Two concepts requiring the same syntax but having different semantics leads to ambiguity unless the programmer differentiates them.”
Let’s assume; I defined the is_contiguous trait. In this case, I can use it to distinguish a random access iterator RA_iter from a contiguous iterator Contiguous_iter.
I can even use a tag class such as is_contiguous into a concept. Now, I have a more straightforward expression of my idea of contiguous iterator Contiguous_iter.
Okay, let me explain two key terms: traits and tag dispatching.
Traits are class templates that extract properties from a generic type.
The following program presents a type that satisfies the specific trait for each of the 14 primary type categories of the type-traits library. The primary type categories are complete and don’t overlap. So each type is a member of a type category. If you check a type category for your type, the request is independent of the const or volatile qualifiers.
I don’t want to bore you to death. Therefore, there is only the output of the line (1).
And here is the output of line (2).
Tag dispatching enables it to choose a function based on the properties of its types. The decision takes place at compile time, and traits which I explained in the last paragraph are used.
A typical example of tag dispatching is the std::advance algorithm from the Standard Template Library. std::advance(it, n) increments the iterator it by n elements. The program shows you the key idea.
The expression std::iterator_traits::iterator_category category determines the iterator category at compile time. Based on the iterator category, the most specific variable of the function advance_impl(i, n, category) is used in line (2). Each container returns an iterator of the iterator category corresponding to its structure. Therefore, line (3) gives a random access iterator, line (4) gives a bidirectional iterator, and line (5) gives a forward iterator which is also an input iterator.
This distinction makes a lot of sense from the performance point of view because a random access iterator can be faster incremented than a bidirectional iterator, and a bidirectional iterator can be faster incremented than an input iterator. From the user’s perspective, you invoke std::advance(it, 5) and get the fastest version your container satisfies.
This was quite verbose. I have not so much to add to the two remaining rules.
The example from the guidelines shows complementary constraints.
Avoid it. Make an unconstrained template and a constrained template instead.
You can even set the unconstrained version to delete so that the constrained versions are only used.
The title for this guideline is quite vague, but the example is self-explanatory.
Instead of using the concepts has_equal and has_not_equal to define the concept Equality
use the usage pattern. This is more readable than the previous version:
In this case the concept Equality requires you to apply == and != to the arguments, and both operations return bool.
Here is a part of the opening from the C++ core guidelines to template interfaces: “…the interface to a template is a critical concept – a contract between a user and an implementer – and should be carefully designed.”. You see, the next post is critical.
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,