C++20: Concepts – What we don’t get
The Template Introduction from the Concepts TS is a new way to use concepts. This syntactic variant is not included in the Concepts Draft and, therefore, in the C++20 standard. But I don’t know what the farther-away future brings.
Template Introduction
Let me first start with a short riddle. Which of the following syntactic variants is not possible with the draft of the concept?
template<typename T> // (1) requires Integral<T> T gcd(T a, T b){ if( b == 0 ) return a; else return gcd(b, a % b); } template<typename T> // (2) T gcd1(T a, T b) requires Integral<T>{ if( b == 0 ){ return a; } else return gcd(b, a % b); } template<Integral T> // (3) T gcd2(T a, T b){ if( b == 0 ){ return a; } else return gcd(b, a % b); } Integral auto gcd3(Integral auto a, Integral auto b){ // (4) if( b == 0 ){ return a; } else return gcd(b, a % b); } Integral{T} // (5) Integral gcd(T a, T b){ if( b == 0 ){ return a; } else{ return gcd(b, a % b); } }
Maybe, you don’t know. So let me name the five variants.
- Requires clause
- Trailing requires clause
- Constrained template parameters
- Abbreviated Function Templates
- Template Introduction
I assume you have chosen the most obscure one: Template Introduction. You are right. Maybe we get it with a later C++ standard, but I’m highly skeptical. In my talks in the last year, I heard no one complaining that they are not part of C++20. I’m also not a big fan of Template Introduction because they introduced a new asymmetry; you know, I’m not a fan of asymmetries.
The Asymmetry
Instead of declaring your constrained template using template<Integral T>, you can see Integral{T}. Here, you see the asymmetry. You can only use Template Introduction with concepts (constrained placeholders) but not with auto (unconstrained placeholders). The following example makes my point. This and the following example is based on the previous concepts’ TS specification and compiled with the GCC.
Modernes C++ Mentoring
Do you want to stay informed: Subscribe.
// templateIntroduction.cpp #include <type_traits> #include <iostream> template<typename T> concept bool Integral(){ return std::is_integral<T>::value; } Integral{T} // (1) Integral gcd(T a, T b){ if( b == 0 ){ return a; } else{ return gcd(b, a % b); } } Integral{T} // (2) class ConstrainedClass{}; /* auto{T} // (4) auto gcd(T a, T b){ if( b == 0 ){ return a; } else{ return gcd(b, a % b); } } auto{T} // (5) class ConstrainedClass{}; */ int main(){ std::cout << std::endl; auto res= gcd(100, 10); ConstrainedClass<int> constrainedClass; ConstrainedClass<double> constrainedClass1; // (3) std::cout << std::endl; }
I use Template introduction for the function template gcd (line 1) and the class template ConstrainedClass (line 2). As expected, the concept will kick in if I try to instantiate ConstraintedClass for double (line 3).
I don’t like it that I can not just replace Integral with auto, such as in lines 4 and 5. Up to this point, I used constrained placeholders (concepts) and unconstrained placeholders (auto) interchangeably in my posts to concepts. This straightforward principle is gone with Template Introduction.
Of course, I could easily overcome this restriction by defining a concept that is always evaluated to true.
// templateIntroductionGeneric.cpp #include <iostream> #include <string> #include <typeinfo> #include <utility> struct NoDefaultConstructor{ // (5) NoDefaultConstructor() = delete; }; template<typename T> // (1) concept bool Generic(){ return true; } Generic{T} // (2) Generic gcd(T a, T b){ if( b == 0 ){ return a; } else{ return gcd(b, a % b); } } Generic{T} // (3) class ConstrainedClass{ public: ConstrainedClass(){ std::cout << typeid(decltype(std::declval<T>())).name() // (4) << std::endl; } }; int main(){ std::cout << std::endl; std::cout << "gcd(100, 10): " << gcd(100, 10) << std::endl; std::cout << std::endl; ConstrainedClass<int> genericClassInt; ConstrainedClass<std::string> genericClassString; ConstrainedClass<double> genericClassDouble; ConstrainedClass<NoDefaultConstructor> genericNoDefaultConstructor; std::cout << std::endl; }
Generic (line 1) is a concept that returns true for all types. Now, I can unify the syntax and define an unconstrained function template (line 4) and an unconstrained class template (line 3). The terms unconstrained or constrained function templates or class templates are not official. I coined them for simplicity reasons.
The expression typeid(decltype(std::declval<T>())).name() (line 4) may look weird to you. std::declvar<T> (C++11) converts the type parameter T into a reference type. Thanks to the reference type, you can use it in a decltype expression to invoke any member function on T without constructing T. It works even for a type T without a default constructor (line 5). In my case (line 4), I invoked the constructor on the reference type to get the string representation of the type parameter T. Here is the program’s output with GCC.
What’s next?
One big topic is left to complete the story concepts: define your concept. Most of the time, you reinvent the wheel because C++20 has many pre-defined concepts. Anyway, I will present in my next post the pre-defined concepts and how you can define your own.
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, 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, Stephen Kelley, Kyle Dean, Tusar Palauri, 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, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery, Matt Godbolt, and Honey Sukesan.
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 |
Modernes C++ GmbH
Modernes C++ Mentoring (English)
Rainer Grimm
Yalovastraße 20
72108 Rottenburg
Mail: schulung@ModernesCpp.de
Mentoring: www.ModernesCpp.org
Modernes C++ Mentoring,
Leave a Reply
Want to join the discussion?Feel free to contribute!