Download presentation
Presentation is loading. Please wait.
1
CS 2304: Templates And Compilation
Gusukuma 2015, credit to Monti, Allevato 2014
2
Gusukuma 2015, credit to Monti, Allevato 2014
Templates Syntactically like Java Generics but VASTLY different However, it’s C++’s version of Generics Templates can be applied to classes AND functions At compile time, creates a copy of the class with a different type E.g. declaring vector<int> myIntList; vector<string> mystrList; Creates TWO vector classes, one for integers and one for strings Gusukuma 2015, credit to Monti, Allevato 2014
3
Templates: Function Example
// typename can be used instead of class template <class T> T sum(const T x, const T y){ return x + y; } For differences between typename and class, go to stack overflow Gusukuma 2015, credit to Monti, Allevato 2014
4
Templates: Class Example
Creates a box class that can use doubles or ints Note that Box<int> and Box<double>creates two COMPLETELY different UNRELATED classes (they don’t even share a base class). template <class T> class Box { public: T get(); void set(T t); private: T elem; }; Gusukuma 2015, credit to Monti, Allevato 2014
5
Templates: Multiple Types
Can have ANY number of types in the parameter list template <class T, class E> class Pair { public: T get1(); void set1(T t); E get2(); void set2(E t); private: T elem; E elem2; }; Gusukuma 2015, credit to Monti, Allevato 2014
6
Templates: Parameter Pack (see cppreference.com)
Variable parameters for templates (0 or more parameters) Functions Usable for variable arguments Use static_assert with type casting Code written LIKE recursion, but not actually recursion Classes/Structs (see eli.thegreenplace.net) Usable for data structures of various size Gusukuma 2015, credit to Monti, Allevato 2014
7
Gusukuma 2015, credit to Monti, Allevato 2014
Templates: Downsides Can’t use the header/source design pattern Templates can’t create an address/shortcut, it’s a “template” for code so the code can’t be written until the type is known Issue of separate compilation units Easily bloats code Code generated for each instantiation of a template Increase compile time Gusukuma 2015, credit to Monti, Allevato 2014
8
Template Compilation Problem
COPY Declaration of myFunc<int> exists? Nope Template declaration of myFunc exists? Yup Creating myFunc<int> from myFunc<T> Definition of myFunc<int> exists? Template Definition for myFunc exists? Creating definition of myFunc<int> from template definition of myFunc<T> Header MyHeader Source1 #include “MyHeader” myFunc<T>{definition} myFunc<int>() Source2 #include “MyHeader” instantitionOfTemplate<double> myFunc<T>() myFunc<T>() Gusukuma 2015, credit to Monti, Allevato 2014
9
Template Compilation Problem
Existing Code myFunc<int>{definition} Template Compilation Problem OK COPY Header MyHeader Source1 #include “MyHeader” myFunc<T>{definition} myFunc<int>() Source2 #include “MyHeader” myFunc<int> myFunc<double> myFunc<T>() myFunc<T>() Gusukuma 2015, credit to Monti, Allevato 2014
10
Template Compilation Problem
Existing Code myFunc<int>{definition} Template Compilation Problem OK Declaration of myFunc<int> exists? Nope, but definition exists COPY COPY Header MyHeader Source1 #include “MyHeader” myFunc<T>{definition} myFunc<int>() Source2 #include “MyHeader” myFunc<int> myFunc<double> myFunc<T>() OK myFunc<T>() myFunc<T>() Gusukuma 2015, credit to Monti, Allevato 2014
11
Template Compilation Problem
Existing Code myFunc<int>{definition} Template Compilation Problem OK COPY Header MyHeader Declaration of myFunc<double> exists? Nope Template declaration of myFunc exists? Yup Creating myFunc<double> from myFunc<T> Definition of myFunc<double> exists? Template Definition for myFunc exists? Source1 #include “MyHeader” myFunc<T>{definition} myFunc<int>() Source2 #include “MyHeader” myFunc<int> myFunc<double> myFunc<T>() myFunc<T>() OK myFunc<T>() OH NOES Gusukuma 2015, credit to Monti, Allevato 2014
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.