By Per Bleshøy Nielsen & Bergur Ziska

Slides:



Advertisements
Similar presentations
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.
Advertisements

David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
Templates 3 Templates and type parameters The basic idea templates is simple: we can make code depend on parameters, so that it can be used in different.
CPSC 252 Templatization Page 1 Templatization In CPSC152, we saw a class vector in which we could specify the type of values that are stored: vector intData(
Washington WASHINGTON UNIVERSITY IN ST LOUIS More on the STL: Function Objects and Generic Algorithms in the STL Fred Kuhns Computer Science and Engineering.
Yan Shi CS/SE 2630 Lecture Notes
Generic Programming in C
C++ Lesson 1.
Pointers and Dynamic Arrays
Programming with ANSI C ++
Chapter 6 CS 3370 – C++ Functions.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
How to be generic Lecture 10
Distinguishing logic from data type
STRICT_VARIANT A simpler variant in C++ Chris Beck
Data types Data types Basic types
Introduction to C++ Systems Programming.
Variadic templates Bjarne Stroustrup Nov 2010.
Type Traits By Reggie Meisler.
Review: Two Programming Paradigms
C++ Templates: More Powerful Than You Think
CS212: Object Oriented Analysis and Design
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Templates.
Reserved Words.
Object-Oriented Programming (OOP) Lecture No. 32
Function and class templates
Lecture 9 Structure 1. Concepts of structure Pointers of structures
ADT Implementations: Templates and Standard Containers
Object-Oriented Programming (OOP) Lecture No. 36
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Initialization List.
Built-In (a.k.a. Native) Types in C++
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Chapter 11 Generic Collections
Introduction to Programming
Java Lesson 36 Mr. Kalmes.
Classes and Objects.
Doubly Linked List Implementation
CISC/CMPE320 - Prof. McLeod
References, const and classes
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Pointers Pointers point to memory locations
Parasol Lab, Texas A&M University
Class.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Yan Shi CS/SE 2630 Lecture Notes
Lab4 problems More about templates Some STL
Chapter 18 Recursion.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
C++ Programming CLASS This pointer Static Class Friend Class
C++ Templates An Introduction to Generic Programming.
C++ Templates CSE 333 Winter 2019
Lists CMSC 202, Version 4/02.
Doubly Linked List Implementation
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
CS100A Sections Dec Loop Invariant Review C Review and Example
Chapter 3 Lists, Stacks, and Queues
Chapter 5 Classes.
Presentation transcript:

By Per Bleshøy Nielsen & Bergur Ziska Tuple Types By Per Bleshøy Nielsen & Bergur Ziska

What is a tuple? A collection of values. Value types can be of same type or may vary. Alternatives: - Arrays - Structs - Reference/pointer arguments

Why tuples? Overcome the limitation of a single return value. Avoid extensive use of pointer / reference parameters. An in-code way of defining small structs.

The Duo We start out with a Duo, which binds a pair of values. This will be the building block for tuples.

Duo example template <typename T1, typename T2> class Duo { public: T1 v1; // value of first field T2 v2; // value of second field // constructors Duo () : v1(), v2() {} Duo (T1 const& a, T2 const& b) : v1(a), v2(b) {} }; int main() { Duo<bool,int> result; result.v1 = true; result.v2 = 42; return 0; }

Recursive Duos We can now use recursion to deal with larger collections of data: Duo<bool,Duo<int,Duo<double, char*> > > result; result.v1 = false; result.v2.v1 = 42; result.v2.v2.v1 = 7.0; result.v2.v2.v2 = ”benny”; Cumbersome access to members We would like a generic way to access members.

The val function // primary template for value of Nth field of (duo) T template < int N, typename T > struct DuoValue { static void get( T& ) { } }; // specialization for 1st field of plain duo template < typename A, typename B > struct DuoValue< 1, Duo< A, B > > { static A& get( Duo< A, B > &d ) { return d.v1; } }; // specialization for Nth field of recursive duo template < int N, typename A, typename B, typename C > struct DuoValue< N, Duo< A, Duo< B, C > > > { static TypeOp< typename DuoT< N-1, Duo< B, C> >::ResultT >::RefT get( Duo< A, Duo< B, C > > &d ) { return DuoValue< N-1, Duo< B, C> >::get( d.v2 ); } };

Easier member access So we implement the val() function using DuoValue: template <int N, typename A, typename B> inline typename TypeOp< typename DuoT<N, Duo<A, B> >::ResultT>::RefT val( Duo<A, B>& d ) { return DuoValue< N, Duo< A, B > >::get( d ); } Now we are able to access members as: Duo< bool, Duo< int, Duo< double, char* > > > result; val<0>(result) = false; val<1>(result) = 42; val<2>(result) = 7.0; val<3>(result) = ”benny”; This eases member access but construction is still cumbersome for large recursive duos

Tuples // type that represents unused type parameters class NullT {}; // Tuple<> in general derives from Tuple<> with one more NullT template <typename P1, typename P2 = NullT, typename P3 = NullT, typename P4 = NullT> struct Tuple : public Duo<P1, typename Tuple<P2,P3,P4,NullT>::BaseT> { typedef Duo<P1, typename Tuple<P2,P3,P4,NullT>::BaseT> BaseT; Tuple() {} Tuple(typename TypeOp<P1>::RefConstT a1, typename TypeOp<P2>::RefConstT a2, typename TypeOp<P3>::RefConstT a3 = NullT(), typename TypeOp<P4>::RefConstT a4 = NullT()) : BaseT(a1, Tuple<P2,P3,P4,NullT>(a2,a3,a4)) { } };

Tuples - specialization // specialization to end deriving recursion template <typename P1, typename P2> class Tuple<P1,P2,NullT,NullT> : public Duo<P1,P2> { public: typedef Duo<P1,P2> BaseT; Tuple() {} Tuple(typename TypeOp<P1>::RefConstT a1, typename TypeOp<P2>::RefConstT a2, typename TypeOp<NullT>::RefConstT = NullT(), typename TypeOp<NullT>::RefConstT = NullT()) : BaseT(a1, a2) { } }; There is also a specialization for singeltons

Pros & cons Fast to write, inplace construction Little or no overhead No descriptive member names Arbitrary size limit, depending on implementation

Sources Vandevoorde, Josuttis: C++ Templates – The Complete Guide