Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Template Metaprogramming Why, When and How? Zoltán Porkoláb Dept. of Programming Languages and Compilers, Faculty of Informatics Eötvös.

Similar presentations


Presentation on theme: "C++ Template Metaprogramming Why, When and How? Zoltán Porkoláb Dept. of Programming Languages and Compilers, Faculty of Informatics Eötvös."— Presentation transcript:

1 C++ Template Metaprogramming Why, When and How? Zoltán Porkoláb gsd@elte.hu Dept. of Programming Languages and Compilers, Faculty of Informatics Eötvös Loránd University, Budapest

2 2CEEPUS Kosice 2007 Agenda I always knew C++ templates were the work of the Devil, and now I'm sure... - Cliff Click cited by Todd Veldhuisen Generative program constructions Metaprograms Power of generative metaprograms Sample usages Open questions

3 3CEEPUS Kosice 2007 Generative constructions Widely used in modern programming languages: Smalltalk, Lisp, other functional langs. Eiffel ADA generics C++ templates Java generics: Pizza, GJ, Java 1.5 C#: soon

4 4CEEPUS Kosice 2007 Why generics? Conventional techniques are working only for complete types. int max( int a, int b) { if ( a > b ) return a; else return b; if ( a > b ) return a; else return b;} double max( double a, double b) { if ( a > b ) return a; else return b; if ( a > b ) return a; else return b;}Etc… class date { //… //… }; date d = max ( d1, d2);

5 5CEEPUS Kosice 2007 Preprocessor Macro? Ada generics : "a restricted form of context-sensitive macro facility" C++ templates: "a clever kind of macro that obeys the scope, naming, and type rules of C++" #define MAX(a,b) a > b ? a : b Works, because a macro is - typeless. Processed not by the compiler, therefore there are a number of "secondary effect": MAX( x, y)*2 x > y ? x : y*2 x > y ? x : y*2 MAX( ++x, y) ++x > y ? ++x : y

6 6CEEPUS Kosice 2007 Generative constructions void swap( int& x, int& y) void swap( int& x, int& y) { int temp = x; // !! how to detect type of x? int temp = x; // !! how to detect type of x? x = y; y = temp; x = y; y = temp; } Does not works: a macro is - typeless. We need a facility to use type parameters. template void swap( T& x, T& y) { T temp = x; x = y; y = temp; } Template depends only on the properties that is actually used Does not require different types used as arguments to be explicitly related. In particular, the argument types used as a template need not be from a single inheritance hierarchy.

7 7CEEPUS Kosice 2007 C++ Function Templates 1. Template is not a single function Rather a schema to instantiate functions on request template T max( T a, T b) { if ( a > b ) return a; if ( a > b ) return a; else return b; else return b;} int i = 3, j = 6, k; k = max(i,j); double x = 3.14, y = 4.15, z; z = max(x,y); Parameter deduction Template instantiation

8 8CEEPUS Kosice 2007 C++ Function Templates 2. Instantiation in compile-time Then strong type system rules are applied int i = 3; double y = 3.14, z; z = max(i,y); template T max( T a, S b) { if ( a > b ) return a; if ( a > b ) return a; else return b; else return b;} z == 3; No deduction on return type No runtime information could be used

9 9CEEPUS Kosice 2007 C++ Function Templates 3. Explicit specialization int i = 3; double y = 3.14, z; template template R max( T a, S b) { if ( a > b ) return a; if ( a > b ) return a; else return b; else return b;} z = max (i,y); Template overloading

10 10CEEPUS Kosice 2007 C++ Function Templates 4. User specialization const char *s1 = “Hello”; const char *s2 = “world”; template <> const char * max( const char *a, const char *b) { if ( strcmp(a,b) < 0 ) return a; if ( strcmp(a,b) < 0 ) return a; else return b; else return b;} The compiler selects the most specific matching type cout << max (4,5); cout (3.14,’6’); cout << max (“this”, “greater”);

11 11CEEPUS Kosice 2007 C++ Class Templates 1. Similar way template class matrix { public: matrix( int i, int j ); matrix( int i, int j ); matrix( const matrix &other) matrix( const matrix &other) ~matrix(); ~matrix(); matrix& operator=( const matrix &other); matrix& operator=( const matrix &other);private: vector v; vector v;}; Created always with explicit specialisation matrix m(4,5);

12 12CEEPUS Kosice 2007 C++ Class Templates 2. User specialization template <> class matrix template <> class matrix { public: matrix( int i, int j ); matrix( int i, int j ); matrix( const matrix &other) matrix( const matrix &other) ~matrix(); ~matrix(); matrix& operator=( const matrix &other); matrix& operator=( const matrix &other);private: a_better_representation v; a_better_representation v;}; Used the same way matrix m(4,5);

13 13CEEPUS Kosice 2007 C++ Templates The C++ templates were first implemented in the early ’90s Accepted as part of the ANSI/ISO in 1994 Erwin Unruh: 1994 unruh.cpp 30: conversion from enum to D requested

14 14CEEPUS Kosice 2007 Power of C++ templates The C++ templates are Turing-complete The Compiler “executes” template metaprograms The result is a non-templated program executed in “run-time” In 1966 Böhm and Jacopini proved: Turing machine implementation conditional and looping constructions Turing machine implementation conditional and looping constructions

15 15CEEPUS Kosice 2007 The Factorial example int factorial( int n) { return (n==0) ? 1 : n*factorial(n-1); return (n==0) ? 1 : n*factorial(n-1);} int main() { cout << factorial(15) << endl; cout << factorial(15) << endl; return 0; return 0;} template struct Factorial { enum { value = N * Factorial ::value }; enum { value = N * Factorial ::value };}; template <> struct Factorial { enum { value = 1 }; enum { value = 1 };}; int main() { const int fact5 = Factorial ::value; const int fact5 = Factorial ::value; std::cout << fact5 << endl; std::cout << fact5 << endl; return 0; return 0;}

16 16CEEPUS Kosice 2007 Conditional statement template template struct IF { typedef Then RET; typedef Then RET;}; template template struct IF struct IF { typedef Else RET; typedef Else RET;}; template template IF ::RET max(T t, S s) { if (t > s) return t; if (t > s) return t; else return s; else return s;}

17 17CEEPUS Kosice 2007 Programs vs. Metaprograms Function (runtime) Data VariableAssignmentConditionLoopClass Type and constant Symbolic names Const initialization enumerated values enumerated valuesRecursion

18 18CEEPUS Kosice 2007 Motivation int main() { const unsigned int di = 12; const unsigned int di = 12; const unsigned int oi = 014; const unsigned int oi = 014; const unsigned int hi = 0xc; const unsigned int hi = 0xc; const unsigned int bi0 = binary_value("1101"); const unsigned int bi0 = binary_value("1101"); const unsigned int bi1 = binary ::value; const unsigned int bi1 = binary ::value;}

19 19CEEPUS Kosice 2007 Motivation template template struct binary { static unsigned const value static unsigned const value = binary ::value * 2 // prepend higher bits = binary ::value * 2 // prepend higher bits + N%10; // to lowest bit + N%10; // to lowest bit}; template <> // specialization struct binary // terminates recursion { static unsigned const value = 0; static unsigned const value = 0;};

20 20CEEPUS Kosice 2007 Main areas Expression templates Static interface checking Regular expression library Concept cheking Extend existing type system ???

21 21CEEPUS Kosice 2007 Expression templates Improve efficiency of programs Save space and / or time Keep the code correctly organized Supercomputing, numerical computing

22 22CEEPUS Kosice 2007 Expression templates Array a, b, c, d, e; // Object-oriented way of a = b + c + d + e double* _t1 = new double[N]; for ( int i=0; i<N; ++i) _t1[i] = b[i] + c[i]; double* _t2 = new double[N]; for ( int i=0; i<N; ++i) _t2[i] = _t1[i] + d[i]; double* _t3 = new double[N*M]; for ( int i=0; i<N; ++i) _t3[i] = _t2[i] + e[i]; for ( int i=0; i<N; ++i) a[i] = _t3[i]; delete [] _t3; delete [] _t2; delete [] _t1; // Fortran like solution: for ( int i=0; i<N; ++i) a[i] = b[i] + c[i] + d[i] + e[i];

23 23CEEPUS Kosice 2007 Concept checking interface Cloneable { T* T::Clone() const; T* T::Clone() const;} template class C { // T must provide T* T::Clone() const... // T must provide T* T::Clone() const...} template class C {public: void SomeFunc( const T* t) { t->Clone(); } void SomeFunc( const T* t) { t->Clone(); }};

24 24CEEPUS Kosice 2007 Extendig type system Family polymorphism Eric Ernst Eric Ernst Generic-Beta Generic-Beta Structural subtyping

25 25CEEPUS Kosice 2007 Structural subtyping Example from Harold Osher Example from Harold Osher OpEval PlusCheckPlusEval Operator Plus OpCheckOpDisplay PlusDisplay

26 26CEEPUS Kosice 2007 Open questions The real expressive power Standard tools but: Loki from Andrei Alexandrescu but: Loki from Andrei AlexandrescuGarantee How to design How to debug

27 C++ Template Metaprogramming Why, When and How? Zoltán Porkoláb gsd@elte.hu Dept. of Programming Languages and Compilers, Faculty of Informatics Eötvös Loránd University, Budapest Thank you! Questions?


Download ppt "C++ Template Metaprogramming Why, When and How? Zoltán Porkoláb Dept. of Programming Languages and Compilers, Faculty of Informatics Eötvös."

Similar presentations


Ads by Google