Concepts revolutionize the way we think about and use generic programming. They didn’t make it in C++11 or C++17 but with C++20, we will get them with a high probability.
Before I write about using concepts, I want to make a general remark.
Too Specific versus Too Generic
Until C++20 we have in C++ two diametral ways to think about functions or user-defined types (classes). Functions or classes can be defined on specific types or on generic types. In the second case, we call them function or class templates. What are the downsides of each way?
It’s quite a job to define a function or class for each specific type. To avoid that burden, type conversion often comes to our rescue but it is also part of the problem. Let’s see what I mean.
You have a function
getInt(int a) that you can invoke with a
double. Now, narrowing conversion takes place.
I assume that is not the behavior you wanted. You started with a
double and ended with an
But conversion works also the other way around.
You have a user-defined type
MyHouse. An instance of
MyHouse can be constructed in two ways. When invoked without an argument (1), its attribute
family is set to an empty string. This means the house is still empty. To easily check if the house is empty or full, I implemented a conversion operator to
bool (2). Fine or? No!
Now, instances of
MyHouse can be used when an
int is required. Strange!
Due to the overloaded operator bool (2), instances of
MyHouse can be used as an
int and can, therefore, be used in arithmetic expressions:
auto res = MyHouse() + 5. This was not my intention! Just for completeness. With C++11, you can declare conversion operators as
explicit. Therefore implicit conversions are not allowed.
My firm belief is that because of convenience reasons, we need the entire magic of conversions in C/C++ to deal with the fact that functions only accept specific arguments.
Are templates the cure? No!
Generic functions or classes can be invoked with arbitrary values. If the values do not satisfy the requirements of the function or class, no problem. You will get a compile-time error. Fine!
What is the problem with this error message?
Of course, it is pretty long and quite challenging to understand, but my crucial concern is a different one. The compilation fails because neither
double or the C-strings supports the % operator. This means the error is due to the failed template instantiation for
double and C-string. This is too late and, therefore, really bad. No template instantiation for type
double or C-strings should be possible. The requirements for the arguments should be part of the function declaration and not a side-effect of an erroneous template instantiation.
Now concepts come to our rescue.
The Third Way
With concepts, we get something in between. We can define functions or classes that act on semantic categories with them. Meaning the arguments of functions or classes are either too specific or too generic but named sets of requirements such as
Sorry for this short post, but one week before my multithreading workshop at the CppCon, I had neither the time nor the resources (no connectivity in the national parks in Washington state) to write an entire post. My next post will be particular because I will write about the CppCon. Afterward, I continue my story about generics and, in particular, about concepts.
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,