C++ templates and Kung Fu training (C++)

Maybe you like martial arts movies as much as I do. (Or even more than me. I never watched a Sonny Chiba triple feature, for example.) When you do, likely you remember these great training montages with cool warm up exercises. Here is a real life example, but movies look always better than real life.It seems pretty good – the protagonist does these air punchings and quadruple kicks and at the end, he will be faster than light and stronger than steel.

Short and sweet, Kung fu training and martial arts training are always cool, and from my point of view, those mental exercises, that help us coding better, seem as cool as Kung fu training. Suppose you are a very great C++ coder, you woke up in the morning light and the day had just began. Likely you don’t do running workout or air punchings, but maybe you do other exercies. These exercises help us being sharp like a razor blade and coding faster than light (without compiler error messages).

All you need is a paper and a pencil, and your job is very simple: you must write complicated templates by heart and without errors. Here is an idea for a small morning workout (it seems in my mind’s eye like roundhouse kicks):

Level-1: Functional template.

static int a = 4; //ugly global variable

template<typename T>
T multiplyByStoredValue(T p) {
  return p * a;
}

Level0: Function of a class template, which is defined outside of a class. (The famous <T>-two-times notation.)

template<typename T>
class Something {
 public:
  explicit Something(int a);
  ~Something();
  T multiplyByStoredValue(T p);
 private:
  int a;
};

template<typename T>
T Something<T>::multiplyByStoredValue(T p) {
  return p * a;
}

Level1: Member function template of a non-template class.

class Something {
 public:
  explicit Something(int a);
  ~Something();

  template<typename T>
  T multiplyByStoredValue(T p) {
    return p * a;
  }
 private:
  int a;
};

Level2: Member function template of a template class.

template<typename T>
class Something {
 public:
  explicit Something(int a);
  ~Something();

  template<typename U>
  U multiplyByStoredValue(U p) {
    return p * a;
  }
 private:
  T a;
};

Level3: Member function template of a template class, defined outside of a class.(The typename-two-times notation.)

template<typename T>
class Something {
 public:
  explicit Something(int a);
  ~Something();

  template<typename U>
  U multiplyByStoredValue(U p);
 private:
  T a;
};

template<typename T> template<typename U>
U void Something<T>::multiplyByStoredValue(U p) {
  return p * a;
}

Level4: Level 3 with mixed style keywords

template<typename T>
class Something {
...
  template<class U>
...
};

template<class T> template<typename U>
...
//yes, it will be compiled/it works without errors

Bonus level: The constructor of a template class.

template<typename T>
class Minimal {
 public:
  explicit Minimal(T a);
 private:
  T a;
};

template<class T>
Minimal<T>::Minimal(T a) : a(a) {
  std::cout << "The following value is stored:" << a << std::endl;
}

Last level: A weird constructor, which parameter has no type.

#include <iostream>
template<typename T>
class Minimal {
 public:
  template<typename U>
  explicit Minimal(U a);
 private:
  T a;
};

template<class T> template<class U>
Minimal<T>::Minimal(U a) : a(a) {
  std::cout << "The following value is stored: "<< a << std::endl;
}

int main() {
  double q(13.5);
  Minimal<int> Minimal(q);
  return 0;
}

Maybe you think I’m one-sided. Well, I hope I’m not. I admire those people too who are doing complicated mental exercises to learn natural languages (and they are not programmers). They are practicing complicated grammatical structures very hard and weird vocabulary words and sometimes they are so cool like Bruce Lee. Once I knew a Polish girl who started to learn Italian (after three other foreign languages), and she read a thick romantic book without any chance to understand it, because her Italian was not very good at the time. She told me, that recognising the frequently used words and listing them is pretty a good exercise. If it isn’t cool, then tell me, what is.

Advertisements

About vrichard86

I'm Richard and I'm an enthusiastic coder. I want to be better and I think a professional journal is a great self improvement tool. Welcome to my blog, to my hq, to my home on the web!
This entry was posted in C++ and tagged , , , , . Bookmark the permalink.