Static Metaprogramming in C++

Static Metaprogramming in C++ Mateus Krepsky Ludwich [email protected] http://www.lisha.ufsc.br September 27, 2010 September 27, 2010 Mateus Kre...
17 downloads 2 Views 314KB Size
Static Metaprogramming in C++

Mateus Krepsky Ludwich [email protected] http://www.lisha.ufsc.br September 27, 2010

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

1

Introduction 

Meta: Greek ● Means: ● E.g.:

● In

"after" or "beyond"

Metaphysics, Metapsychology

linguistics: just means "being about" something

● E.g.:

A metalanguage is a language to describe another language



Metaprograms ● Programs

that represent and manipulate other programs or themselves

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

2

Metaprograms execution Post-runtime

Static metaprograms

Compile time Information from static analysis only

Linking

Loading

Runtime

Complete execution history

Dynamic metaprograms September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

3

Dynamic Metaprogramming 

Reflection ● ...”The

ability of a program to manipulate as data something representing the state of a program during its own execution.” [Gabriel, B. W., 1993]



Reification ● Encoding



Introspection ● Observe



state as data

/ reason about its own state

Intercession ● Modify

its own execution state or its own interpretation or meaning

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

4

Examples of levels of reflection 

Smalltalk ● Meta-objects:

provide language concepts (methods, classes, execution stacks, the processor) in the form of libraries ● High level of reflection 

Java ● Reflection

API: used to discover methods and attributes at runtime ● No direct modification of classes or methods 

C++ ● RTTI: Runtime ● Dynamic cast

September 27, 2010

type information

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

5

Static Metaprogramming  

“Run“ before load time of the code they manipulate – usually compile time Most common examples ● Compilers ● AST

→ Assembly

● Preprocessors ● Ling1

September 27, 2010

→ Ling2

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

6

Types of SMP 

Open Compilers ● May

provide access to its parts (parser, code generator) ● E.g.:

OpenC++, MPC++, Magik, Xroma

● Transformation ● Provides



systems

an interface to write transformations on AST

Two-level languages ● Static code: “runs” at compile time ● Dynamic code: runs at runtimme ● E.g.: Templates C++

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

7

C++ as Two-Level Language 

Static code ● Templates ●*



e.g.: conditional operator: “?”

Dynamic code ● “ordinary”

features)



+ other C++ features*

C++ (all the others constructions and

Static code (subset of C++) is Turing-complete ● Conditional construction → Template specialization ● Loop construction → Template recursion

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

8

Factorial example 

Dynamic factorial

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

9

Factorial example 

Static factorial

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

10

“Functional flavor of the static level” Class templates as functions

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

11

“Functional flavor of the static level” Class templates as functions Integer and types as data

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

12

“Functional flavor of the static level” Class templates as functions Integer and types as data Template recursion instead of loops

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

13

“Functional flavor of the static level” Class templates as functions Integer and types as data Template recursion instead of loops Constant initialization instead of assignment

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

14

Template Metaprogramming

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

15

Template Metaprogramming

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

16

Template Metaprogramming

factorial

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

17

Template Metaprogramming

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

18

Template Metaprogramming

IF

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

19

Metaprogrammed IF

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

20

Template Metaprogramming

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

21

Template Metaprogramming

Recursive code expansion

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

22

Recursive code expansion

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

23

Recursive code expansion power(m,n) power(m)

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

24

Template Metaprogramming

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

25

Template Metaprogramming Lists and Trees as Nested templates

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

26

Metaprogrammed list (cons 1 (cons 2 (cons 3 (cons 9 nil)))) Lisp

creates: [1, 2, 3, 9] Cons

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

C++

27

Metaprogrammed list

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

28

Template Metaprogramming

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

29

Expression templates 

Allows for optimized code generation for adding a number of vectors ● V4

= v1 + v2 + v3

● Simply

overloading the + operator is inefficient (temporary vector for each +)



Can be used to implement compile-time domain-specific checks ● E.g.

“an expression cannot contain more than five + operators”



Useful to implement domain-specific languages

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

30

Conclusions 

Metaprogramming ● Dynamic: reflection ● Static: template metaprogramming



Static metaprogramming in C++ ● Turing-complete ● Metainformation ● Metafunction ● Expression templates

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

31

References 

Czarnecki, K. and Eisenecker, U. W. 2000 Generative Programming: Methods, Tools, and Applications. ACM Press/Addison-Wesley Publishing Co. ● Chapter



10: Static Metaprogramming in C++

Stroustrup, B. 2000 The C++ Programming Language. 3rd. Addison-Wesley Longman Publishing Co., Inc. ● Chapter

September 27, 2010

13: Templates

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

32

Exercises 

Implementing fibonacci metafunction

September 27, 2010

Mateus Krepsky Ludwich (http://www.lisha.ufsc.br)

33

Suggest Documents