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 <type_traits>
#include <iostream>

template<typename T>
concept bool Integral(){
  return std::is_integral<T>::value;
}

template<typename T>
requires Integral<T>()
T gcd(T a, T b){
  if( b == 0 ){ return a; }
  else{
    return gcd(b, a % b);
  }
}

template<Integral T>
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.

 conceptsIntegralVariations

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

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 <iostream>
#include <typeinfo>

template<typename T>
concept bool Integral(){
  return std::is_integral<T>::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.

placeholders

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 <type_traits>
#include <iostream>

template<typename T>
concept bool Integral(){
  return std::is_integral<T>::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<int> constrainedClass;
  ConstrainedClass<double> 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).

templateIntroductionError

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 <iostream>
#include <string>
#include <typeinfo>
#include <utility>


template<typename T>
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<T>())).name() << 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;
  
  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. 

templateIntroductionGeneric

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, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Louis St-Amour, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Tobi Heideman, Daniel Hufschläger, Red Trip, Alexander Schwarz, Tornike Porchxidze, Alessandro Pezzato, Evangelos Denaxas, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Satish Vangipuram, and Michael Dunsky.

 

Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, and Said Mert Turkal.

 

 

My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small

 

Seminars

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

Bookable (Online)

German

Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

New

Contact Me

Modernes C++,

RainerGrimmSmall

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Interactive Course: The All-in-One Guide to C++20

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 5960

Yesterday 11310

Week 28244

Month 188918

All 6837610

Currently are 159 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments