





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The concept of templates in C++ and how they support generic programming. Templates provide a simple way to represent general concepts and combine them with various data types. They offer advantages such as more general code, less bugs, and no runtime overhead. the use of templates, template instantiation, and template constructors and member functions.
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!
Use of Templates
Templates in C++
Template Instantiation
class
class
class
class< >
Instantiations Object code does not exist until template is instantiated •template is included as a header file
Template is instantiated at compile time
Warning: Do not generate different templates for objects withErrors in template not detected until instantiation
Results in ‘code-bloat’ => big increase in executable size.only minor differences
Template Instantiation
template
// define my template member functions here};
mytemplate.hh
void myprogram(){#include “mytemplate.hh” mytemplate
myprogram.cc
class myderivedclass: public mytemplate
myderivedclass.hh
Inheritance works as before: derived class sees public/protectedTemplate is instantiated when class is compiled => object code
Note: as before for classes. All constructors/destructors/member functions declared Template Constructors & Member Functions
{ : // setup member data from argstemplate
// other initialisation
template
int mytemplate
mytemplate.hh
Only Constructors/Member Functions which are used are instantiated
Using Templates
as = ts.function(); // member functionStringC as;mytemplate
myprogram.cc
Object code isProgram instantiates construction and function()
(^) only (^) compiled for constructor and function()
Template code for otherfunction() is
(^) not (^) compiled
Function Templates
Specify a generic function •avoids run-time overheads of type checking or virtual function•works for a broad range of types
void swap(DataC& a, DataC& b){template
swap.hh
void myprogram(){#include “swap.hh” swap(i,j);double u(5.5), v(9.8);int i(5), j(10);
// j=5 i=
swap(i,u);swap(u,v); // u=9.8 v=5.
(^) // invalid different types - implicit cast
myprogram.cc
More Templates...
};template<template
Designing & Implementing Templates
Using separate files .hh & .tcc
mytemplate.hh
single header file
header file for declaration
mytemplate.hh
#include “mytemplate.tcc”};template
// include file for definition
header file for definition
mytemplate.tcc