In my last post, C++20: Two Extremes and the Rescue with Concepts, I gave the first motivation for concepts. Concepts put semantic constraints on template parameters. Today, I present different use-cases for concepts in a compact form.
Just keep it in mind: What are the advantages of concepts?
- Requirements for templates are part of the interface.
- The overloading of functions or specialization of class templates can be based on concepts.
- We get an improved error message because the compiler compares the requirements of the template parameter with the actual template arguments
- You can use predefined concepts or define your own.
- The usage of auto and concepts is unified. Instead of auto, you can use a concept.
- If a function declaration uses a concept, it automatically becomes a function template. Writing function templates is, therefore, as easy as writing a function.
This post is about the first three points. Let me show many different usages of concepts:
There are three ways to use the concept Sortable. For simplicity reasons, I only show the declaration of the function template.
Trailing Requires Clause
Constrained Template Parameters
The algorithm sort requires, in this case, that the container is sortable. Sortable has to be a constant expression and a predicate.
You can define a class template that only accepts objects.
The compiler complains that a reference is not an object. Maybe you wonder what an object is.? A possible implementation of the type-traits function std::is_object answers:
An object is either a scalar, an array, a union, or a class.
In this case, the member function requires the template parameter T to be copyable.
You can use concepts in variadic templates. The definition of the function templates is based on fold expressions. all, any, and none require from it type parameter T that has to support the concept Arithmetic. Arithmetic essential means that T is either integral or floating-point.
The brand-new Microsoft compiler 19.23 partially supports the concepts syntax.
Of course, you can use more than one requirement for the template parameters.
The function template find requires that the container S is a SequenceContainer and its elements are EqualityComparable.
std::advance(iter, n) puts its iterator iter n position further. Depending on the iterator, the implementation can use pointer arithmetic or go n times further. In the first case, the execution time is constant; in the second case, the execution time depends on the stepsize n. Thanks to concepts, you can overload std::advance on the iterator category.
Based on the iterator category, the containers std::vector, std::list, and std::forward_list support, the best fitting std::advance implementation is used.
Concepts also support template specializations.
MyVector<int&> goes to the unconstrained template parameter.
MyVector<int> goes to the constrained template parameter.
My next post is about syntactical unification in C++20. With C++20, you can use a constrained placeholder (concept) in each place. You could use an unconstrained placeholder (auto) in C++11. But this is not the end of the unification. Defining a template becomes with C++20 a piece of cake. Just use a constrained or an unconstrained placeholder to declare a function.
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,