when it needs to be done at compile-time

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Templates CS-341 Dick Steflik. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply certain attributes.
Sentinel Logic Assumes while loops and input statements.
C++ Template Metaprogramming Why, When and How? Zoltán Porkoláb Dept. of Programming Languages and Compilers, Faculty of Informatics Eötvös.
Programming Language Support for Generic Libraries Jeremy Siek and Walid Taha Abstract The generic programming methodology is revolutionizing the way we.
CPS120: Introduction to Computer Science Functions.
Meta IFL 2007 Freiburg1 Meta - Towards a Functional-Style Interface for C++ Template Metaprograms * Ádám Sipos, Zoltán Porkoláb, Norbert Pataki, Viktória.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Methods.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
5th MaCS Debrecen On the Turing-Completeness of Generative Metaprograms Zoltán Porkoláb, István Zólyomi Dept. of Programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
CO4301 – Advanced Games Development Week 1 Introduction
TK1924 Program Design & Problem Solving Session 2011/2012
to understand recursion you must understand recursion
Test 2 Review Outline.
Topic: Recursion – Part 2
Chapter 13: Overloading and Templates
Scope and Code Generation
Ch 10- Advanced Object-Oriented Programming Features
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Inheritance and Polymorphism
FUNCTIONS In C++.
Methods Chapter 6.
Chapter 14 Templates C++ How to Program, 8/e
Introduction to Custom Templates
Methods Attributes Method Modifiers ‘static’
CS212: Object Oriented Analysis and Design
Templates.
This pointer, Dynamic memory allocation, Constructors and Destructor
to understand recursion you must understand recursion
Formatted and Unformatted Input/Output Functions
Important Concepts from Clojure
Important Concepts from Clojure
AVG 24th 2015 ADVANCED c# - part 1.
User Defined Functions
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Chapter 4 Functions Objectives
Lecture 17 Recursion part 1 Richard Gesick.
Conditional Statements
Name: Rubaisha Rajpoot
Inheritance and Polymorphism:
Functions, Part 1 of 3 Topics Using Predefined Functions
Abstraction: Generic Programming, pt. 2
Compiler Code Optimizations
Unit 3 Test: Friday.
Exception Handling.
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Java Programming: Chapter 9: Recursion Second Edition
When a function is called...
Functions, Part 1 of 3 Topics Using Predefined Functions
Chapter 14 Abstract Classes and Interfaces
In C Programming Language
Lecture 12 Recursion part 1 CSE /26/2018.
Important Concepts from Clojure
Yan Shi CS/SE 2630 Lecture Notes
Predefined Functions Revisited
Functions, Part 1 of 3 Topics Using Predefined Functions
Function Templates Class Templates
Corresponds with Chapter 5
Templates Generic Programming.
Lecture 20 – Practice Exercises 4
Presentation transcript:

when it needs to be done at compile-time Metaprogramming when it needs to be done at compile-time

Metaprogramming programming to be executed at compile-time in C++ done with templates, hence template metaprogramming (TMP) optimizes code at compile-time, possibly faster compilation is slower some programming work is delegated to compiler resultant code is easier(?) to understand

Preliminaries decltype(expression) – returns type of expression, C++11 addition recursive template – a template may be invoked inside the template stopping case provided as specialization non-type parameter for template instantiation – used for template recursion treated as a constant std::function is defined in <functional> is used to create function types, to be used as template parameters syntax std::function<returnValueType (argTypes…)> example std::function <void(int, const string&)> f1 = func;

Template Specialization may need to implement specific actions for particular type specialization – overrides generic classes or method may specialize either whole template template<> class Stack<char, 100> { … }; or individual methods void Stack<char>::input(){…} why not use non-templated classes for specific types? specialization provides uniform interface to template users may only need specialization on individual methods

Examples branching factorial loop unrolling