SlideShare a Scribd company logo
Thin Template By Somenath Mukhopadhyay [email_address]
What is a Template Compile time polymorphism Use of a single declaration for multiple types provided the logic is the same for all of the supported types
Example of Template Declaration: Template <class T> T Add (T& a, T& b)‏ { Return a + b; } Usage: Int x = T<int> Add (1 , 2)‏ Float y = T<float> Add (1.1 , 2.2)
Disadvantage of standard template For each type it will produce one set of Add function Large code foot print For devices having small amount of memory, it is not acceptable
Thin Template in the rescue It is based upon the assumption that most optimized compilers will generate the code only for template functions that actually use different types. Example Template <class T> class CTest { int Test1(void) { return 1;} T Test2(void) { return 1;} };
Thin Template in the rescue CTest < char> oClass1; CTest <int> oClass2; Only one code will be generated for function Test1(), because this function doesn’t use template argument T and contains the same code in both versions However for the function Test2(), two versions will be generated for the two different types, one for char and the other for int. This is because this function uses argument T as a return type
Thin Template implementation logic Most modern compilers like Microsoft C++ compiler or GCC have this capability. The main idea behind “thin template” is to move all code that does not use template arguments into separate functions, so that only one instance will be generated for them.  To implement thin template, we usually have a Base Class containing all the template-independent functionalities; and a derived template class from it containing template-dependent structures and code.
Thin Template Example We will define a template class CList CList is derived from a non-template class CListThin  CList  class has a template function called Add, which will add an item to the end of the list This Add function will in turn take the help of the CListThin's non-template function AddThin. Thus the boilerplate code of moving the pointer to the end of the list has been implemented by the non-template function AddThin and the actual adding of the template data has been implemented by the template function Add.
Thin Template Example This is shown in the following class diagram
Thin Template Example CListThin Class definition class CListThin { protected: typedef struct SThinItem { SThinItem* prev; SThinItem* next; }; public: CListThin():iFirstItem(0),iLastItem(0){}; void AddThin(SThinItem& aDestData); private: SThinItem* iFirstItem; SThinItem* iLastItem; };
Thin Template Example CListThin class implementation void CListThin::AddThin(SThinItem &aDestData)‏ { aDestData.prev = iLastItem ? iLastItem : 0; aDestData.next = 0; if(iLastItem) iLastItem->next = &aDestData; iLastItem = &aDestData; if(!iFirstItem) iFirstItem = &aDestData; }
Thin Template Example CList Class Implementation template <class T> class CList : public CListThin { public: CList(){} bool Add(T& aDestData); private: typedef struct SItem : public CListThin::SThinItem { T data; }; }; template<class T> bool CList<T> :: Add(T& aDestData)‏ { SItem* item = new SItem; if(!item)  return false; item->data = aDestData; AddThin(*item); return true; }
Thin Template and Design Pattern Thin Template  is one of the applications of the Design Pattern called Template Method (please refer the GoF book for further information)‏
Reference Using &quot;Thin Templates&quot; to Reduce Application Size by  Gregor Petrov http://www.codeguru.com/cpp/cpp/cpp_mfc/tutorials/article.php/c7927/
Thank You

More Related Content

TXT
CORE JAVA
PPTX
Type casting in java
PDF
Functions
TXT
CORE JAVA
PPTX
Type conversion
PPT
C# Method overloading
PPTX
Constructor and destructor
PDF
Intake 38 5
CORE JAVA
Type casting in java
Functions
CORE JAVA
Type conversion
C# Method overloading
Constructor and destructor
Intake 38 5

What's hot (20)

DOC
14review(abstract classandinterfaces)
PDF
Generic Programming
PPTX
if, while and for in Python
PPT
Generic Programming seminar
PDF
Aanchal Gupta,BCA 2nd year
PPTX
Polymorphism
PPT
Modern Compiler Design
PDF
Oop sample ktu
ODT
(3) cpp abstractions more_on_user_defined_types_exercises
PDF
Rakesh Bijawat, BCA 2nd Year
PDF
Rakesh Bijawat , BCA Third Year
PPTX
Primitives in Generics
PDF
Deepak Soni,BCA 2nd Year
PDF
Pinkle Makhijani ,BCA 2nd Year
PPT
Static and dynamic polymorphism
PDF
Intake 38 data access 5
PDF
Ram Prasad ,BCA 2nd Year
PPT
Covariance, contravariance 觀念分享
PPTX
Interface and abstraction
PPTX
2CPP18 - Modifiers
14review(abstract classandinterfaces)
Generic Programming
if, while and for in Python
Generic Programming seminar
Aanchal Gupta,BCA 2nd year
Polymorphism
Modern Compiler Design
Oop sample ktu
(3) cpp abstractions more_on_user_defined_types_exercises
Rakesh Bijawat, BCA 2nd Year
Rakesh Bijawat , BCA Third Year
Primitives in Generics
Deepak Soni,BCA 2nd Year
Pinkle Makhijani ,BCA 2nd Year
Static and dynamic polymorphism
Intake 38 data access 5
Ram Prasad ,BCA 2nd Year
Covariance, contravariance 觀念分享
Interface and abstraction
2CPP18 - Modifiers
Ad

Similar to Thin Template Explained (20)

PPT
Advanced Programming C++
PPT
Lecture 04 - Templates.ppt,Class Templates and Function templates
PPT
Savitch Ch 17
PPT
Templates exception handling
PPT
Savitch ch 17
PPTX
Templates presentation
PPT
Chapter 1 Presentation
PDF
[gbgcpp] Let's get comfortable with concepts
PPT
Unit iii
PPTX
Templates2
PDF
4.1 C++ Template for engineering course. Learn easily
DOCX
unit 5.docx...............................
PPTX
CSharp_03_Generics_introduction_withexamples
PDF
Let's get comfortable with C++20 concepts - Cologne C++ User group
PPT
Attributes & .NET components
PDF
PPT
Introduction to csharp
PPT
Introduction to csharp
PPT
Introduction to-csharp-1229579367461426-1
PPT
Introduction To Csharp
Advanced Programming C++
Lecture 04 - Templates.ppt,Class Templates and Function templates
Savitch Ch 17
Templates exception handling
Savitch ch 17
Templates presentation
Chapter 1 Presentation
[gbgcpp] Let's get comfortable with concepts
Unit iii
Templates2
4.1 C++ Template for engineering course. Learn easily
unit 5.docx...............................
CSharp_03_Generics_introduction_withexamples
Let's get comfortable with C++20 concepts - Cologne C++ User group
Attributes & .NET components
Introduction to csharp
Introduction to csharp
Introduction to-csharp-1229579367461426-1
Introduction To Csharp
Ad

More from Somenath Mukhopadhyay (20)

PDF
Significance of private inheritance in C++...
PDF
Arranging the words of a text lexicographically trie
PDF
Generic asynchronous HTTP utility for android
PDF
Copy on write
PDF
Java concurrency model - The Future Task
PDF
Memory layout in C++ vis a-vis polymorphism and padding bits
PDF
Developing an Android REST client to determine POI using asynctask and integr...
PDF
Observer pattern
PDF
Uml training
PDF
How to create your own background for google docs
PDF
The Designing of a Software System from scratch with the help of OOAD & UML -...
PDF
Structural Relationship between Content Resolver and Content Provider of Andr...
PDF
Flow of events during Media Player creation in Android
PDF
Implementation of a state machine for a longrunning background task in androi...
PDF
Tackling circular dependency in Java
PDF
Implementation of composite design pattern in android view and widgets
PDF
Exception Handling in the C++ Constructor
PDF
Active object of Symbian in the lights of client server architecture
PDF
Android services internals
PDF
Android Asynctask Internals vis-a-vis half-sync half-async design pattern
Significance of private inheritance in C++...
Arranging the words of a text lexicographically trie
Generic asynchronous HTTP utility for android
Copy on write
Java concurrency model - The Future Task
Memory layout in C++ vis a-vis polymorphism and padding bits
Developing an Android REST client to determine POI using asynctask and integr...
Observer pattern
Uml training
How to create your own background for google docs
The Designing of a Software System from scratch with the help of OOAD & UML -...
Structural Relationship between Content Resolver and Content Provider of Andr...
Flow of events during Media Player creation in Android
Implementation of a state machine for a longrunning background task in androi...
Tackling circular dependency in Java
Implementation of composite design pattern in android view and widgets
Exception Handling in the C++ Constructor
Active object of Symbian in the lights of client server architecture
Android services internals
Android Asynctask Internals vis-a-vis half-sync half-async design pattern

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Electronic commerce courselecture one. Pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
KodekX | Application Modernization Development
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPT
Teaching material agriculture food technology
PDF
Approach and Philosophy of On baking technology
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Empathic Computing: Creating Shared Understanding
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Spectroscopy.pptx food analysis technology
Encapsulation theory and applications.pdf
Chapter 3 Spatial Domain Image Processing.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Programs and apps: productivity, graphics, security and other tools
Electronic commerce courselecture one. Pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
KodekX | Application Modernization Development
Spectral efficient network and resource selection model in 5G networks
Machine learning based COVID-19 study performance prediction
Understanding_Digital_Forensics_Presentation.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Teaching material agriculture food technology
Approach and Philosophy of On baking technology
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Unlocking AI with Model Context Protocol (MCP)
Empathic Computing: Creating Shared Understanding
The AUB Centre for AI in Media Proposal.docx
sap open course for s4hana steps from ECC to s4
MYSQL Presentation for SQL database connectivity
Spectroscopy.pptx food analysis technology

Thin Template Explained

  • 1. Thin Template By Somenath Mukhopadhyay [email_address]
  • 2. What is a Template Compile time polymorphism Use of a single declaration for multiple types provided the logic is the same for all of the supported types
  • 3. Example of Template Declaration: Template <class T> T Add (T& a, T& b)‏ { Return a + b; } Usage: Int x = T<int> Add (1 , 2)‏ Float y = T<float> Add (1.1 , 2.2)
  • 4. Disadvantage of standard template For each type it will produce one set of Add function Large code foot print For devices having small amount of memory, it is not acceptable
  • 5. Thin Template in the rescue It is based upon the assumption that most optimized compilers will generate the code only for template functions that actually use different types. Example Template <class T> class CTest { int Test1(void) { return 1;} T Test2(void) { return 1;} };
  • 6. Thin Template in the rescue CTest < char> oClass1; CTest <int> oClass2; Only one code will be generated for function Test1(), because this function doesn’t use template argument T and contains the same code in both versions However for the function Test2(), two versions will be generated for the two different types, one for char and the other for int. This is because this function uses argument T as a return type
  • 7. Thin Template implementation logic Most modern compilers like Microsoft C++ compiler or GCC have this capability. The main idea behind “thin template” is to move all code that does not use template arguments into separate functions, so that only one instance will be generated for them. To implement thin template, we usually have a Base Class containing all the template-independent functionalities; and a derived template class from it containing template-dependent structures and code.
  • 8. Thin Template Example We will define a template class CList CList is derived from a non-template class CListThin CList class has a template function called Add, which will add an item to the end of the list This Add function will in turn take the help of the CListThin's non-template function AddThin. Thus the boilerplate code of moving the pointer to the end of the list has been implemented by the non-template function AddThin and the actual adding of the template data has been implemented by the template function Add.
  • 9. Thin Template Example This is shown in the following class diagram
  • 10. Thin Template Example CListThin Class definition class CListThin { protected: typedef struct SThinItem { SThinItem* prev; SThinItem* next; }; public: CListThin():iFirstItem(0),iLastItem(0){}; void AddThin(SThinItem& aDestData); private: SThinItem* iFirstItem; SThinItem* iLastItem; };
  • 11. Thin Template Example CListThin class implementation void CListThin::AddThin(SThinItem &aDestData)‏ { aDestData.prev = iLastItem ? iLastItem : 0; aDestData.next = 0; if(iLastItem) iLastItem->next = &aDestData; iLastItem = &aDestData; if(!iFirstItem) iFirstItem = &aDestData; }
  • 12. Thin Template Example CList Class Implementation template <class T> class CList : public CListThin { public: CList(){} bool Add(T& aDestData); private: typedef struct SItem : public CListThin::SThinItem { T data; }; }; template<class T> bool CList<T> :: Add(T& aDestData)‏ { SItem* item = new SItem; if(!item) return false; item->data = aDestData; AddThin(*item); return true; }
  • 13. Thin Template and Design Pattern Thin Template is one of the applications of the Design Pattern called Template Method (please refer the GoF book for further information)‏
  • 14. Reference Using &quot;Thin Templates&quot; to Reduce Application Size by Gregor Petrov http://www.codeguru.com/cpp/cpp/cpp_mfc/tutorials/article.php/c7927/