# Placeholders - The Second

Contents[Show]

The unification of templates, concepts, and placeholders goes on. This time, I will have a closer look at constrained (concepts) and unconstrained (auto) placeholders in the context of templates.

At first, a short reminder for you (and me). I stated in my last post Concepts -Placeholders: You can use constrained placeholders (concepts) in each situation where you can use unconstrained placeholders (auto). I used in the last post the concept Integral in the algorithm gcd to make it more type-safe. Let's continue.

## Syntactic sugar and more

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53``` ```// conceptsIntegralVariations.cpp #include #include template concept bool Integral(){ return std::is_integral::value; } template requires Integral() T gcd(T a, T b){ if( b == 0 ){ return a; } else{ return gcd(b, a % b); } } template T gcd1(T a, T b){ if( b == 0 ){ return a; } else{ return gcd(b, a % b); } } Integral gcd2(Integral a, Integral b){ if( b == 0 ){ return a; } else{ return gcd(b, a % b); } } auto gcd3(auto a, auto b){ if( b == 0 ){ return a; } else{ return gcd(b, a % b); } } int main(){ std::cout << std::endl; std::cout << "gcd(100, 10)= " << gcd(100, 10) << std::endl; std::cout << "gcd1(100, 10)= " << gcd1(100, 10) << std::endl; std::cout << "gcd2(100, 10)= " << gcd2(100, 10) << std::endl; std::cout << "gcd3(100, 10)= " << gcd3(100, 10) << std::endl; std::cout << std::endl; } ```

Don't get me wrong. I'm a big fan of syntactic sugar. Syntactic sugar makes a feature easier to use and you can, therefore, write more expressive and less error-prone code. Isn't C++ only syntactic sugar for C or assembler?

I define in line 7 -9 the concept Integral that I use in the algorithm gcd (line 11 - 18). Now,  I make the syntax sweeter. Instead of specifying the concept in the required clause (line 12), I can use it instead of the keyword typename or class for the type parameter (line 20). It gets even sweeter. The concept Integral is used in line 28 as a kind of function parameter. Of course, gcd2 becomes by the concept of a function template. Now the unifications kicks in. If I replace a constrained placeholder (Integral) in line 28 with the unconstrained placeholders (auto) in line 35, I will get an unconstrained function template. Sorry about my new coined term unconstrained function template. I mean, gcd3 is a template that can accept values of arbitrary type.

To wrap up my sentences, here is the output of the program.

I called this section "Syntactic sugar and more". But why more? You will see.

## Modernes C++ Mentoring

Stay informed about my mentoring programs.

Subscribe via E-Mail.

### Syntactic sugar and more

The function templates gcd2 is more powerful than the function template gcd or gcd1. gcd2 has two type parameters that have not to be the same. Both have to respect the concept of Integral. The same observations hold for the function template gcd3. a and b have not to be of the same type. The usage of the function template twoTypes in line 39 makes my point clear.

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43``` ```// placeholders.cpp #include #include template concept bool Integral(){ return std::is_integral::value; } void overload(auto t){ std::cout << "auto : " << t << std::endl; } void overload(Integral t){ std::cout << "Integral : " << t << std::endl; } void overload(long t){ std::cout << "long : " << t << std::endl; } void twoTypes(auto a, auto b){ std::cout << typeid(a).name() << std::endl; std::cout << typeid(b).name() << std::endl; } int main(){ std::cout << std::endl; overload(3.14); overload(2010); overload(2020l); std::cout << std::endl; twoTypes(2010, 3.14); std::cout << std::endl; } ```

With a little bit of Run Time Type Infomation (RTTI) in line 24 and 25, I get a string representation of the types. As expected, the types are int and double.

But the short example shows more. The interplay between the unconstrained function template (line 11), the constrained function template (line 15), and the function (line 19) is very smooth. That is exactly the behaviour, I expected.

In case you hate the classical way to declare template, there is a new way.

## Template introduction

Instead of declaring your constrained template by using template<Integral T>, you can now just right Integral{T}. Once more, I wrote constrained templates. So, you have to use concepts.

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49``` ```// templateIntroduction.cpp #include #include template concept bool Integral(){ return std::is_integral::value; } Integral{T} Integral gcd(T a, T b){ if( b == 0 ){ return a; } else{ return gcd(b, a % b); } } Integral{T} class ConstrainedClass{}; /* auto{T} auto gcd(T a, T b){ if( b == 0 ){ return a; } else{ return gcd(b, a % b); } } auto{T} class ConstrainedClass{}; */ int main(){ std::cout << std::endl; auto res= gcd(100, 10); ConstrainedClass constrainedClass; ConstrainedClass constrainedClass1; std::cout << std::endl; } ```

I used the new syntax for the function template gcd in line 11 and the class-template ConstrainedClass in line 19. The concept will kick in if I try to instantiate ConstraintedClass for double (line 45).

To be honest,  I don't like it that I can not just replace Integral with auto such as in line 24 to 33. Up to this point in my posts, I have used a constrained placeholder instead of an unconstrained placeholder and the other way around. That is a simple principle.

Of course, I can overcome this restriction by using a concept, that evaluates always to true.

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45``` ```// templateIntroductionGeneric.cpp #include #include #include #include template concept bool Generic(){ return true; } Generic{T} Generic gcd(T a, T b){ if( b == 0 ){ return a; } else{ return gcd(b, a % b); } } Generic{T} class ConstrainedClass{ public: ConstrainedClass(){ std::cout << typeid(decltype(std::declval())).name() << std::endl; } }; int main(){ std::cout << std::endl; std::cout << "gcd(100, 10): " << gcd(100, 10) << std::endl; std::cout << std::endl; ConstrainedClass genericClassInt; ConstrainedClass genericClassString; ConstrainedClass genericClassDouble; std::cout << std::endl; } ```

Generic (line 9 - 12) is my concept that returns true for all types. Now, I can unify the syntax and define an unconstrained function-template (line 14 - 20) and an unconstrained class-template (line 22 - 28). Sorry for the expression typeid(decltype(std::declval<T>())).name() in line 26 that works even for a type T without default constructor. This expression returns the string representation of the type parameter T. Here is the output of the program.

## What's next?

I always use the same concept Integral in my examples. Bored? That will change. I will write in my next post about defining 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, Animus24, 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, and Dominik Vošček.

Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, and Slavko Radman.

My special thanks to PVS-Studio

My special thanks to Tipi.build

## Seminars

I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

### 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++

#### New

• Clean Code with Modern C++
• C++20

Tags: Concepts

### Subscribe to the newsletter (+ pdf bundle)

 Email Please enable the javascript to submit this form

### Visitors

Today 639

Yesterday 6519

Week 37201

Month 181372

All 11662526

Currently are 161 guests and no members online

• #### How can you recognise a good software architecture?

Modern code: "Naming is hard", so bad names are used. Comments forget to comment so are removed.

• #### How can you recognise a good software architecture?

Code should be as expressive as possible and ideally should be readable without any comments. As ...

• #### How can you recognise a good software architecture?

I began developing software in the late 1960's and have been at it ever since. The one thing missing ...

• #### The C++ Standard Library: The Fourth Edition includes C++23

Hi, In India from where do I buy hard copy of The C++ Standard Library: The Fourth Edition includes C++23 ...

• #### The Iterator Protocol

I think an std::forward_iterator has to return a reference from it's operator* (). This would mean a ...