Day 7 - Hmmmm


Let's revise something.
If I want the some class for char, int, float, double etc. I must have more classes like that but each of them is made especially for char, int, float, ...
All that uses memory. Well, where would it be anyway.
Each generated class compiler optimise for char, int, float, ... . Today's compilers are miracle worker.
Generally I know how they work and I know that they are mercyless when the optimise.
Some clever people developed a theory. They've been working on it for years, so who am I to think about it for five minutes( actually 7 days ) and say it doesn't work.

Let's get on with my problem.

We have MyClass and Generator_MyClas.
That means for each new class I must make apropriate #define.
One class but two used names.
Now let's see how it would be like i.e. work as template:
 template < typename N_value >
  class MyClass
   {
    public:
     MyClass( N_value P_a ): M_a( P_a ){}

     void clac(){ std::cout << "Do not disturb! calc in progress ..." << std::endl; }

    private:
     N_value a;
   };

Old header from day 6 would be somethig similar to this:
file: MyClass_char.hpp
 #ifndef _HPP_MyClass_char
  #define _HPP_MyClass_char
  #include "MyClass.hpp"
   typedef MyClass<char> MyClass_char;
 #endif
Or it would be better to put them all into one:
 typedef MyClass<char>   MyClass_char;
 typedef MyClass<int>    MyClass_int;
 typedef MyClass<float>  MyClass_float;
 typedef MyClass<double> MyClass_double;
No problem with long double and short int.
 typedef MyClass<long double>  MyClass_long_double;
 typedef MyClass<short int>    MyClass_short_int;
Nor with:
 typedef MyClass<MyClass<float> >    MyClass_MyClass_float;
Compiler translates it. It desn't matter because it desn't make sense..

It looks very nice, doesn't it? No more copy/paste, making up new names ( Generator_MyClass, typedef short int short_int ).

Simple as that.
How will compiler translate it?
I dont care it's his problem.
Compiler will do it well, very well.
It can be done the way I did it using #define but I think compiler will use better idea.
Conclusion: Template rulez!!!