1 An anomaly of subtype relations at component refinement and a generative solution in C++ Zoltán Porkoláb, István Zólyomi {gsd | Eötvös.

Slides:



Advertisements
Similar presentations
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
Brown Bag #3 Return of the C++. Topics  Common C++ “Gotchas”  Polymorphism  Best Practices  Useful Titbits.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
DEV 321 Understanding and Using Advanced C++ Template Features and Topics on ISO C++ Scott Currie Program Manager Visual C++ Microsoft Corporation.
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Programming Languages and Paradigms
5th MaCS Debrecen On the Turing-Completeness of Generative Metaprograms Zoltán Porkoláb, István Zólyomi Dept. of Programming.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Inheritance. Today: Inheritance and derived classes Is-A relationship class hierarchies proper inheritance (pure) polymorphism virtual functions protected.
Chapter 14: Overloading and Templates
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Encapsulation by Subprograms and Type Definitions
2 Objectives You should be able to describe: Operator Functions Two Useful Alternatives – operator() and operator[] Data Type Conversions Class Inheritance.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
1 An extension to the subtype relationship in C++ István Zólyomi, Zoltán Porkoláb and Tamás Kozsik {scamel | gsd | Eötvös Loránd University,
Chapter 12: Adding Functionality to Your Classes.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Names Variables Type Checking Strong Typing Type Compatibility 1.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Compiler Support for Profiling C++ Template Metaprograms József Mihalicza, Norbert Pataki, Zoltán Porkoláb Eötvös Loránd University Faculty of Informatics.
C++ Template Metaprogramming Why, When and How? Zoltán Porkoláb Dept. of Programming Languages and Compilers, Faculty of Informatics Eötvös.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
GPCE'04, Vancouver 1 Towards a General Template Introspection Library in C++ István Zólyomi, Zoltán Porkoláb Department of Programming Languages and Compilers.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
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.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Ch. 5 Ch. 51 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (more notes) Dr. Carter Tiernan.
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
Object-Oriented Programming Chapter Chapter
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Ch. 5 Ch. 51 jcmt Summer 2003Programming Languages CSE3302 Programming Languages (more notes) Summer 2003 Dr. Carter Tiernan.
Principles of Object-Oriented Software Development The language C++
Inheritance and Composition Reusing the code and functionality Unit - 04.
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see.
Overview of C++ Polymorphism
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Abstract Classes & Object Slicing. Object Slicing.
Csontos Péter, Porkoláb Zoltán Eötvös Loránd Tudományegyetem, Budapest ECOOP 2001 On the complexity of exception handling.
DGrid: A Library of Large-Scale Distributed Spatial Data Structures Pieter Hooimeijer,
FASTFAST All rights reserved © MEP Make programming fun again.
5th MaCS Debrecen On the Turing-Completeness of Generative Metaprograms Zoltán Porkoláb, István Zólyomi Dept. of Programming.
Chapter 2 Objects and Classes
Names and Attributes Names are a key programming language feature
Type Checking, and Scopes
Programming with ANSI C ++
Review: Two Programming Paradigms
CS212: Object Oriented Analysis and Design
Programming with ANSI C ++
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Overview of C++ Polymorphism
Object Slicing & Dynamic Casts
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

1 An anomaly of subtype relations at component refinement and a generative solution in C++ Zoltán Porkoláb, István Zólyomi {gsd | Eötvös Loránd University, Budapest, Hungary

2 OpEval PlusCheckPlusEval Operator Plus OpCheckOpDisplay PlusDisplay Example (Harold Ossher)

3 Possible solutions Virtual inheritance Intrusive: specified in base classes More concerns implies exponential number of virtual bases Traits No subtype relationship using hierarchies in traits AOP are extensions to standard C++ Signature Promising, but non-standard, available only for Gnu compiler Eiffel: ADOPT CSet: subtype relationship implemented with metaprogramming Non-intrusive solution based on the C++ standard

4 C++ Templates Macro-like type parameters –Strong type checking Templates alone form a Turing-complete functional language inside C++ –Based on template specializations Metaprograms –Executed during compilation –A program can observe its state –May make decisions depending on state –Compile-time adaptation

5 CSET_3(OpDisplay, OpEval, OpCheck) op;

6

7 Source code of Cset construction template struct CSet; template struct CSet > : public Head, public CSet { // --- Type name shortcuts typedef CSet Rest; typedef CSet > MyType; // --- Copy constructor CSet(const MyType& mt) : Head(mt), Rest(mt) {} // --- "Recursive" constructor CSet(const Head& head, const Rest& rest): Head(head), Rest(rest) {} }; template struct CSet > : public Head { // --- All in one constructor CSet(const Head& head) : Head(head) {} };

8 Conversions between CSets Initialize the head class for each recursion step Template constructors provide conversion steps (the same for operator= ) CSET_3(PlusEval,PlusDisplay, PlusCheck) sum; CSET_2(OpEval,OpCheck) calculate; calculate = sum; Example of usage: template template CSet > :: CSet(const FromType& f): Head(f), CSet (f) {}

9 Advantages Type safe: based on builtin language conversions Efficient: no temporal CSets or objects are used, objects are initialized directly Open: not restricted to CSet only, any user object can be converted without explicit conversion call. struct Minus: public MinusEval, public MinusDisplay, public MinusCheck {}; Minus subtract; calculate = subtract;

10 Smart pointers Problems: Conversion copies objects by value (slicing) No dynamic binding Solution: smart pointers Implementation and usage similar to those of CSet CSET_3(PlusDisplay, PlusEval, PlusCheck) sum; CSETPTR_2(OpDisplay, OpEval) opPtr(sum); // --- Function call with explicit cast static_cast (opPtr)->show(); // --- In longer form with implicit cast OpDisplay *displayPtr = opPtr; displayPtr->show();

11 CSETPTR_2(OpDisplay,OpEval) OpDisplay *head CSETPTR_1(OpEval) OpEval *head PlusDisplayPlusCheckPlusEval points to CSETPTR_2(OpDisplay, OpEval) opPtr; head: OpDisplay* head: OpEval*

12 Similar to CSetPtr Store references instead of pointers Consequences: A CSetRef object must be initialized Initializes by reference but copies by value during assignment CSetRef CSET_3(PlusDisplay, PlusEval, PlusCheck) sum; // --- Initializes by reference CSETREF_3(OpDisplay, OpEval, OpCheck) exprRef(sum); // --- Copies by value exprRef = sum;

13 #include #include cset.h" struct Shape { virtual void f() { std::cout << "Shape"; } }; struct Circle : public Shape { void f() { std::cout << "Circle"; } }; struct Colored {}; struct Filled {}; void main() { CSET_3(Circle, Colored, Filled) extCircle; CSET_3(Colored, Filled, Shape) extShape(extCircle); CSET_2(Filled, Colored) extensions(extCircle); extensions = extShape = extCircle; CSETPTR_3(Colored, Filled, Shape) extShapePtr(extCircle); Shape* shapePtr = extShapePtr; shapePtr->f(); // --- Prints Circle CSETPTR_3(Filled, Colored, Circle) extCirclePtr(extCircle); extShapePtr = extCircle;// --- Object -> Pointer extShapePtr = extCirclePtr; // --- Der. Pointer -> Base Pointer extShape = extShapePtr;// --- Pointer -> Object (by value) }

14 Summary Support for collaboration based design Non-intrusive solution Easy to understand and manage No extension to standard C++ Bad experience with compilers vs standards Features: Composition of concerns in a single class (CSet) Conversion between related CSets Dynamic binding with smart pointers and references

15 Zoltán Porkoláb István Zólyomi Eötvös Loránd University, Budapest, Hungary Download source from