Template%20MetaProgramming%20in%20C - PowerPoint PPT Presentation

About This Presentation
Title:

Template%20MetaProgramming%20in%20C

Description:

Fact i ::RET; // does not work (const i works) Meta Operators ... cout Comb 2, 4 ::RET endl; Representing Data Structures. template int n, class U ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 18
Provided by: giuseppe92
Category:

less

Transcript and Presenter's Notes

Title: Template%20MetaProgramming%20in%20C


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

2
Meta Language
Meta Program
Analysis
Generation
3
Example
  • templateltint igt
  • class C
  • enum RES i
  • cout ltlt Clt2gtRES

4
Turing Equivalent language
  • template ltint ngt
  • struct Fact
  • enum RET n Factltn-1gtRET
  • template ltgt
  • struct Factlt0gt
  • enum RET 1
  • Factlt5gtRET // 120
  • int i 3
  • FactltigtRET // does not work (const i works)

5
Meta Operators
  • template ltbool g, class T, class Egt
  • struct IF
  • typedef T RET
  • template ltclass T, class Egt
  • struct IFltfalse, T, Egt
  • typedef E RET

6
Example
  • class CompactingGC
  • void GC()
  • class MarkAndSweepGC
  • void GC()
  • IF ltGC COPY, CopyingGC, MarkAndSweepGCgtRET
    gc
  • gc.GC()

7
Other Operators
  • struct FalseType
  • struct TrueType
  • template ltclass Tgt
  • struct isPointer typedef FalseType RET
  • template ltclass Tgt
  • struct isPointerltTgt
  • typedef TrueType RET

8
Conditional
  • templateltint n1, int n2gt
  • struct Max
  • enum RET (n1 gt n2) ? n1 n2

9
Template Metafunctions
  • C(k, n) n! / (k! (n-k)!)
  • templateltint k, int ngt
  • struct Comb
  • enum RET FactltngtRET /
  • (FactltkgtRET Factltn-kgtRET)
  • cout ltlt Comblt2, 4gtRET ltlt endl

10
Representing Data Structures
  • template ltint n, class Ugt
  • struct List
  • enum item n
  • typedef U next
  • struct Nil
  • typedef V Listlt1, Listlt2, Nil()gt gt
  • Vitem 1
  • Vnextitem 2
  • Vnextnext Nil

11
Functions on Templates
  • template ltclass Lgt
  • struct Length
  • enum RET 1 LengthltLnextgtRET
  • template ltgt
  • struct LengthltNilgt enum RET 0

12
Possible Uses
  • Control loop unrolling
  • FORlt3, Bgtloop

13
Unrolling vector addition
  • inline void addVect(int size, double a, double
    b, double c)
  • while (size--)
  • c a b

14
Template Unrolling
  • templateltint sizegt
  • inline void addVect(double a, double b, double
    c)
  • c a b
  • addVectltsize-1gt(a1, b1, c1)
  • templateltgt
  • inline void addVectlt0gt(double a, double b,
    double c)
  • addVectlt3gt(a, b, c)

15
Effect of unrolling
  • addVectlt3gt(a, b, c)
  • c a b
  • addVectlt2gt(a1, b1, c1)
  • (c1) (a1) (b1)
  • addVectlt1gt(a2, b2, c2)
  • (c2) (a2) (b2)
  • addVectlt0gt(a3, b3, c3)

16
Loop Unrolling
  • templateltint n, class Bgt
  • struct FOR
  • static void loop(int m)
  • for (int i 0 i lt m/n i)
  • UNROLLltn, Bgtiteration(i n)
  • for (int i 0 i lt mn i)
  • Bbody(n m/n i)

17
Loop Unrolling (2)
  • template ltint n, class Bgt
  • struct UNROLL
  • static void iteration(int i)
  • Bbody(i)
  • UNROLLltn-1, Bgtiteration(i 1)
  • template ltclass Bgt
  • struct UNROLLlt0, Bgt
  • static void iteration(int i)
Write a Comment
User Comments (0)
About PowerShow.com