Presentation is loading. Please wait.

Presentation is loading. Please wait.

Template MetaProgramming in C++ Giuseppe Attardi Università di Pisa.

Similar presentations


Presentation on theme: "Template MetaProgramming in C++ Giuseppe Attardi Università di Pisa."— Presentation transcript:

1 Template MetaProgramming in C++ Giuseppe Attardi Università di Pisa

2 Meta Language Meta Program AnalysisGeneration

3 Example template template class C { enum { RES = i }; }; cout ::RES;

4 Turing Equivalent language template template struct Fact { enum { RET = n * Fact ::RET }; }; template <> struct Fact { enum { RET = 1 }; }; Fact ::RET;// == 120 int i = 3; Fact ::RET;// does not work (const i works)

5 Meta Operators template template struct IF { typedef T RET; }; template template struct IF { typedef E RET; };

6 Example class CompactingGC { void GC() { … } }; class MarkAndSweepGC { void GC() { … } }; IF ::RET gc; gc.GC();

7 Other Operators struct FalseType {}; struct TrueType {}; template template struct isPointer { typedef FalseType RET; }; template template struct isPointer { typedef TrueType RET; };

8 Conditional template template struct Max { enum { RET = (n1 > n2) ? n1 : n2 }; }

9 Template Metafunctions C(k, n) = n! / (k! * (n-k)!) template template struct Comb { enum { RET = Fact ::RET / enum { RET = Fact ::RET / (Fact ::RET * Fact ::RET) }; }; cout ::RET ::RET << endl;

10 Representing Data Structures template template struct List { enum { item = n }; typedef U next; }; struct Nil { } ; typedef V List >; V::item == 1; V::next::item == 2; V::next::next == Nil;

11 Functions on Templates template template struct Length { enum { RET = 1 + Length ::RET }; }; template <> struct Length { enum RET = 0 };

12 Possible Uses Control loop unrolling: Control loop unrolling: FOR ::loop;

13 Unrolling vector addition inline void addVect(int size, double* a, double* b, double* c) { while (size--) while (size--) *c++ = *a++ + *b++; *c++ = *a++ + *b++;}

14 Template Unrolling template template inline void addVect(double* a, double* b, double* c) { *c = *a + *b; *c = *a + *b; addVect (a+1, b+1, c+1); addVect (a+1, b+1, c+1);}template<> inline void addVect (double* a, double* b, double* c) { } addVect (a, b, c);

15 Effect of unrolling addVect (a, b, c); *c = *a + *b; *c = *a + *b; addVect (a+1, b+1, c+1); addVect (a+1, b+1, c+1); *(c+1) = *(a+1) + *(b+1); *(c+1) = *(a+1) + *(b+1); addVect (a+2, b+2, c+2); addVect (a+2, b+2, c+2); *(c+2) = *(a+2) + *(b+2); *(c+2) = *(a+2) + *(b+2); addVect (a+3, b+3, c+3); addVect (a+3, b+3, c+3);

16 Loop Unrolling template template struct FOR { static void loop(int m) { for (int i = 0; i < m/n; i++) { UNROLL ::iteration(i * n); } for (int i = 0; i < m%n; i++) { B::body(n * m/n + i); }};

17 Loop Unrolling (2) template template struct UNROLL { static void iteration(int i) { B::body(i); UNROLL ::iteration(i + 1); }}; template template struct UNROLL { static void iteration(int i) { } };


Download ppt "Template MetaProgramming in C++ Giuseppe Attardi Università di Pisa."

Similar presentations


Ads by Google