Nicolás Brailovsky


A modern blog

Template metaprogramming V: Face to face

author Posted by: nico on date May 12th, 2010 | filed Filed under: C++, Templates
By now we have learned the basics for a nice template metaprogramming toolkit:

  • Loops with recursive template definitions
  • Conditionals with partial template specializations
  • Returns using typedefs

Unfortunately that’s all you need for a Turing complete language, meaning now we have the power, bwahahaha! Mph, I’m sorry, back on topic, it means we can now create a fully functional and useful template metaprogramming device… for approximating e, nonetheless. Oh, you think that’s not useful? Well though luck, that’s all you get for now:

  1. template  struct Frak {
  2.         static const long Num = N;
  3.         static const long Den = D;
  4. };
  5.  
  6. template  struct MultEscalar {
  7.         typedef Frak< N*X::Num, N*X::Den > result;
  8. };
  9.  
  10. template  struct IgualBase {
  11.         typedef typename MultEscalar< X1, Y1::Den >::result X;
  12.         typedef typename MultEscalar< Y1, X1::Den >::result Y;
  13. };
  14.  
  15. template        struct MCD {
  16.         static const long result = MCD::result;
  17. };
  18. template  struct MCD {
  19.         static const long result = X;
  20. };
  21.  
  22. template  struct Simpl {
  23.         static const long mcd = MCD::result;
  24.         typedef Frak< F::Num / mcd, F::Den / mcd > result;
  25. };
  26.  
  27. template  struct Suma {
  28.         typedef IgualBase B;
  29.         static const long Num = B::X::Num + B::Y::Num;
  30.         static const long Den = B::Y::Den; // == B::X::Den
  31.         typedef typename Simpl< Frak >::result result;
  32. };
  33.  
  34. template  struct Fact {
  35.         static const long result = N * Fact::result;
  36. };
  37. template <> struct Fact<0> {
  38.         static const long result = 1;
  39. };
  40.  
  41. template  struct E {
  42.         // e = S(1/n!) = 1/0! + 1/1! + 1/2! + …
  43.         static const long Den = Fact::result;
  44.         typedef Frak< 1, Den > term;
  45.         typedef typename E::result next_term;
  46.         typedef typename Suma< term, next_term >::result result;
  47. };
  48. template <> struct E<0> {
  49.         typedef Frak<1, 1> result;
  50. };
  51.  
  52. #include
  53. int main() {
  54.         typedef E<8>::result X;
  55.         std::cout << "e = " << (1.0 * X::Num / X::Den) << "\n";
  56.         std::cout << "e = " << X::Num <<"/"<< X::Den << "\n";
  57.         return 0;
  58. }

Looking nice, isn’t it? You should have all what’s needed to understand what’s going on there. Even more, almost everything has been explained in previous articles, with the exception of EqBase. But that’s left as an exersice for the reader because the writer is too lazy.

If you think any part of the code requires clarification ask in the comments. Next, a long overdue topic: lists using template metaprogramming. Guaranteed to blow your mind into little pieces!

     Add A Comment

trackback Trackback URI | rsscomment Comments RSS