{"id":951,"date":"2025-06-10T12:42:25","date_gmt":"2025-06-10T09:42:25","guid":{"rendered":"https:\/\/www.certbolt.com\/certification\/?p=951"},"modified":"2025-12-31T14:15:12","modified_gmt":"2025-12-31T11:15:12","slug":"demystifying-templates-in-c","status":"publish","type":"post","link":"https:\/\/www.certbolt.com\/certification\/demystifying-templates-in-c\/","title":{"rendered":"Demystifying Templates in C++"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Templates in C++ serve as the cornerstone of generic programming. They allow programmers to create functions and classes that operate with any data type. Rather than duplicating code for each data type, templates offer a single definition that works with various types. This significantly reduces code redundancy and enhances code maintainability. Templates enable a developer to write more flexible and reusable code. When a template is instantiated, the compiler generates the appropriate function or class by replacing the generic type with the actual data type provided.<\/span><\/p>\n<p><b>The Need for Templates<\/b><\/p>\n<p><span style=\"font-weight: 400;\">In traditional programming, different data types require separate functions or classes even if the underlying logic is the same. For instance, sorting integers, floats, or strings would typically need separate functions. Templates overcome this issue by generalizing the code structure. This capability is invaluable in scenarios where the same algorithm needs to be implemented across multiple data types. Templates streamline development and lead to cleaner and more consistent code.<\/span><\/p>\n<p><b>Basic Syntax of Templates<\/b><\/p>\n<p><span style=\"font-weight: 400;\">C++ templates use the keyword <\/span><span style=\"font-weight: 400;\">template<\/span><span style=\"font-weight: 400;\"> followed by template parameters enclosed in angle brackets. These parameters typically use the <\/span><span style=\"font-weight: 400;\">typename<\/span><span style=\"font-weight: 400;\"> or <\/span><span style=\"font-weight: 400;\">class<\/span><span style=\"font-weight: 400;\"> keyword to denote generic types. Here is the basic syntax for a function template:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;class T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T functionName(T arg1, T arg2) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\/\/ function body<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Similarly, the syntax for a class template is:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class ClassName {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\/\/ class body<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The <\/span><span style=\"font-weight: 400;\">typename<\/span><span style=\"font-weight: 400;\"> and <\/span><span style=\"font-weight: 400;\">class<\/span><span style=\"font-weight: 400;\"> keywords are interchangeable, although <\/span><span style=\"font-weight: 400;\">typename<\/span><span style=\"font-weight: 400;\"> is more commonly used in modern C++.<\/span><\/p>\n<p><b>Function Templates in Detail<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Function templates enable functions to work with generic types. When a function template is called with specific types, the compiler generates a version of the function with those types. This is known as template instantiation. Consider the following example that returns the maximum of two values:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;class T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T MaxVal(T a, T b) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return (a &gt; b)? a: b;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In this example, the function can work with integers, floats, or characters. When <\/span><span style=\"font-weight: 400;\">MaxVal&lt;int&gt;(5, 10)<\/span><span style=\"font-weight: 400;\"> is called, the compiler generates a version of <\/span><span style=\"font-weight: 400;\">MaxVal<\/span><span style=\"font-weight: 400;\"> specifically for integers. This approach eliminates the need for multiple overloaded functions.<\/span><\/p>\n<p><b>Example: Function Template Usage<\/b><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;iostream&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">using namespace std;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;class T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T MaxVal(T a, T b) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return (a &gt; b) a: b;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int main() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; MaxVal&lt;int&gt;(5, 8) &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; MaxVal&lt;double&gt;(6.5, 4.0) &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; MaxVal&lt;char&gt;(&#8216;f&#8217;, &#8216;k&#8217;) &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return 0;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This program demonstrates how one function can be used across multiple data types, simplifying the implementation and improving code readability.<\/span><\/p>\n<p><b>Advantages of Function Templates<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Function templates offer several advantages:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">They reduce code duplication.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> They simplify maintenance<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> They make code more readable and expressive.e<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> They enable type safety during compilation.on<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> Templates are particularly useful in libraries where generic data manipulation is needed.<\/span><\/p>\n<p><b>Class Templates in Detail<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Class templates allow the definition of classes that can operate with any data type. This is especially useful for creating container classes like arrays, lists, and stacks that should handle various types. A class template is defined similarly to a function template but applies to an entire class:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;class T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Array {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Private:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0T* data;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0int size;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Array(T arr[], int s) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0data = new T[s];<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0size = s;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for (int i = 0; i &lt; size; ++i)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0data[i] = arr[i];<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void print() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for (int i = 0; i &lt; size; ++i)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; data[i] &lt;&lt; &#187; &#171;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This class can be instantiated with any data type, such as int, float, or custom classes.<\/span><\/p>\n<p><b>Example: Class Template Usage<\/b><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;iostream&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">using namespace std;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;class T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Array {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">private:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0T* pointr;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0int size;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Array(T arr[], int s) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0pointr = new T[s];<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0size = s;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for (int i = 0; i &lt; size; i++)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0pointr[i] = arr[i];<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void print() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for (int i = 0; i &lt; size; i++)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; pointr[i] &lt;&lt; &#187; &#171;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int main() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0int arr[5] = {1, 2, 3, 4, 5};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Array&lt;int&gt; a(arr, 5);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0a.print();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return 0;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The above example shows a class that stores and prints an array. The same class can be used for any data type.<\/span><\/p>\n<p><b>Advantages of Class Templates<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Class templates enhance flexibility and reusability<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> They provide strong type-checking at compile time<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> They facilitate the development of a generic container<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> Templates encourage writing abstract, generalized code<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> Class templates are foundational in building libraries like the Standard Template Library (STL)<\/span><\/p>\n<p><b>How Templates Work at Compile Time<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Templates are expanded at compile time through a process called template instantiation. When the compiler encounters a function or class template call with a specific type, it generates the corresponding function or class definition. Unlike macros, templates support type checking, which ensures that only valid types are used. This leads to safer and more robust code.<\/span><\/p>\n<p><b>Template Parameters<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Templates can accept different types of parameters:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Type parameters: the most common, specified using <\/span><span style=\"font-weight: 400;\">typename<\/span><span style=\"font-weight: 400;\"> or <\/span><span style=\"font-weight: 400;\">class<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> Non-type parameters, such as integers or characters, which are useful for fixed-size arrays<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> Template template parameters: allow templates to accept other templates as arguments<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example of non-type parameter:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;class T, int size&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class FixedArray {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0T arr[size];<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This creates a fixed-size array of a specified data type and size.<\/span><\/p>\n<p><b>Template Specialization in C++<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Template specialization in C++ allows the programmer to provide a specific implementation of a template when a particular data type is used. While generic templates provide a generalized solution, there are situations where specific types need a tailored approach. Template specialization addresses this by allowing a unique definition for specific data types while maintaining the general template structure for others.<\/span><\/p>\n<p><b>Why Use Template Specialization<\/b><\/p>\n<p><span style=\"font-weight: 400;\">There are cases where generic behavior does not meet all requirements. For example, a class that prints elements of different types might need to handle strings differently from integers or floats. In such scenarios, template specialization allows for fine-tuning the behavior of the template for specific types, providing more control and enhancing the flexibility of the code.<\/span><\/p>\n<p><b>Syntax of Template Specialization<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Template specialization is declared using the same template name but specifies the data type for which the specialized implementation is intended. Below is the syntax for template specialization:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">void functionName&lt;int&gt;(int arg) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\/\/ specialized implementation for int<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This tells the compiler to use this version of the function when the data type is <\/span><span style=\"font-weight: 400;\">int<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><b>Example: Function Template Specialization<\/b><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;iostream&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">using namespace std;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">void display(T value) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; &#171;Generic template: &#187; &lt;&lt; value &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">void display&lt;int&gt;(int value) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; &#171;Specialized template for int: &#187; &lt;&lt; value &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int main() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0display(10);\u00a0 \u00a0 \u00a0 \u00a0 \/\/ uses specialized version<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0display(&#8216;A&#8217;); \u00a0 \u00a0 \u00a0 \/\/ uses generic version<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0display(5.5); \u00a0 \u00a0 \u00a0 \/\/ uses generic version<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return 0;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This code demonstrates how <\/span><span style=\"font-weight: 400;\">display&lt;int&gt;<\/span><span style=\"font-weight: 400;\"> is treated differently due to specialization.<\/span><\/p>\n<p><b>Class Template Specialization<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Similar to functions, class templates can also be specialized. Class template specialization is useful when a particular data type needs a distinct class behavior or structure. Below is the general syntax:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class ClassName&lt;int&gt; {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\/\/ specialized implementation for int<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><b>Example: Class Template Specialization<\/b><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;iostream&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">using namespace std;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Printer {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void print(T value) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; &#171;Generic print: &#187; &lt;&lt; value &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Printer&lt;string&gt; {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void print(string value) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; &#171;String print: &#187; &lt;&lt; value &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int main() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Printer&lt;int&gt; p1;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0p1.print(100);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Printer&lt;string&gt; p2;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0p2.print(&#171;Hello&#187;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return 0;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This example shows how the <\/span><span style=\"font-weight: 400;\">Printer<\/span><span style=\"font-weight: 400;\"> class behaves differently for <\/span><span style=\"font-weight: 400;\">string<\/span><span style=\"font-weight: 400;\"> types.<\/span><\/p>\n<p><b>Partial Specialization<\/b><\/p>\n<p><span style=\"font-weight: 400;\">C++ also supports partial specialization of class templates. Partial specialization allows customizing certain parts of the template while retaining the generic nature for other parts. It provides a middle ground between full specialization and full generalization.<\/span><\/p>\n<p><b>Syntax of Partial Specialization<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Partial specialization is defined by specifying only some of the template parameters:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T, typename U&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Container {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\/\/ generic definition<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Container&lt;T, int&gt; {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\/\/ partially specialized definition for the second type as int<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><b>Example: Partial Specialization<\/b><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;iostream&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">using namespace std;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T, typename U&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Container {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void display() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; &#171;Generic Container&#187; &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Container&lt;T, int&gt; {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void display() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; &#171;Partial specialization where second type is int&#187; &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int main() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Container&lt;float, char&gt; c1;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0c1.display();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Container&lt;float, int&gt; c2;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0c2.display();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return 0;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This example illustrates how partial specialization is used when only some template parameters need customization.<\/span><\/p>\n<p><b>Multiple Template Parameters<\/b><\/p>\n<p><b>Declaring Multiple Parameters<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Templates in C++ are not limited to a single parameter. You can pass multiple parameters to a template to handle complex data or mixed types. This allows the creation of more versatile and flexible templates.<\/span><\/p>\n<p><b>Syntax for Multiple Parameters<\/b><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T, typename U&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class ClassName {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\/\/ class body<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This syntax indicates that the class or function works with two different data types.<\/span><\/p>\n<p><b>Example: Template with Two Parameters<\/b><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;iostream&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">using namespace std;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;class T, class U&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Pair {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">private:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0T first;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0U second;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Pair(T f, U s) : first(f), second(s) {}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void show() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; &#171;First: &#187; &lt;&lt; first &lt;&lt; &#171;, Second: &#187; &lt;&lt; second &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int main() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Pair&lt;int, double&gt; obj1(10, 15.75);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0obj1.show();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Pair&lt;string, char&gt; obj2(&#171;Text&#187;, &#8216;A&#8217;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0obj2.show();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return 0;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This example demonstrates how a class can be made more functional by supporting multiple data types.<\/span><\/p>\n<p><b>Default Template Arguments<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Like functions, templates in C++ can have default arguments. This provides more flexibility, allowing the template to be used with or without specifying all the arguments. If an argument is not provided, the default value is used.<\/span><\/p>\n<p><b>Syntax for Default Arguments<\/b><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T, typename U = char&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Sample {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\/\/ class body<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here, <\/span><span style=\"font-weight: 400;\">U<\/span><span style=\"font-weight: 400;\"> will default to <\/span><span style=\"font-weight: 400;\">char<\/span><span style=\"font-weight: 400;\"> if not specified.<\/span><\/p>\n<p><b>Example: Using Default Arguments<\/b><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;iostream&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">using namespace std;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T, typename U = char&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Example {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0T x;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0U y;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Example() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; &#171;Constructor Called&#187; &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int main() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Example&lt;int&gt; obj1;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Example&lt;int, float&gt; obj2;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return 0;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In this case, <\/span><span style=\"font-weight: 400;\">obj1<\/span><span style=\"font-weight: 400;\"> uses <\/span><span style=\"font-weight: 400;\">int<\/span><span style=\"font-weight: 400;\"> and defaults <\/span><span style=\"font-weight: 400;\">U<\/span><span style=\"font-weight: 400;\"> to <\/span><span style=\"font-weight: 400;\">char<\/span><span style=\"font-weight: 400;\">, while <\/span><span style=\"font-weight: 400;\">obj2<\/span><span style=\"font-weight: 400;\"> explicitly uses both <\/span><span style=\"font-weight: 400;\">int<\/span><span style=\"font-weight: 400;\"> and <\/span><span style=\"font-weight: 400;\">float<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><b>Templates vs Function Overloading<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Templates and function overloading both support polymorphism in C++, but they serve different purposes. Function overloading is used when the logic is similar but needs tailored implementations for different types. Templates are best when the logic is identical across types.<\/span><\/p>\n<p><b>Function Overloading<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Function overloading involves creating multiple functions with the same name but different parameter lists:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">void print(int a) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; a &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">void print(double b) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; b &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This method provides control, but can become verbose when dealing with many types.<\/span><\/p>\n<p><b>Templates for Polymorphism<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Templates allow a single function to handle multiple types:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">void print(T value) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; value &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This approach reduces code duplication and is easier to maintain.<\/span><\/p>\n<p><b>Choosing Between the Two<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Use templates when the function logic is truly generic<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> Use function overloading when different types require distinct logic<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Templates are more suitable for libraries and generic data structures, while overloading is better for small, type-specific customizations.<\/span><\/p>\n<p><b>Template Metaprogramming in C++<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Template metaprogramming (TMP) is a powerful technique in C++ where templates are used to perform computations at compile time rather than at runtime. This approach leverages the compiler&#8217;s template instantiation mechanism to execute code during compilation, enabling optimizations and complex code generation before the program runs.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">TMP allows developers to write code that can generate other code or make decisions at compile time, improving performance and enabling static checks that would otherwise require runtime overhead.<\/span><\/p>\n<p><b>Why Use Template Metaprogramming?<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Using template metaprogramming can improve performance by eliminating calculations during runtime and catching errors early during compilation. It enables the creation of highly generic and optimized libraries and supports advanced techniques such as static assertions, conditional compilation, and type manipulation.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Common use cases include calculating factorials or Fibonacci numbers, type traits for detecting properties of types, and implementing compile-time lists or algorithms.<\/span><\/p>\n<p><b>Basic Example: Compile-Time Factorial Calculation<\/b><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;iostream&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">using namespace std;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;int N&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">struct Factorial {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0static const int value = N * Factorial&lt;N &#8212; 1&gt;::value;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">struct Factorial&lt;0&gt; {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0static const int value = 1;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int main() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; &#171;Factorial of 5 is: &#187; &lt;&lt; Factorial&lt;5&gt;::value &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return 0;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In this example, the factorial of 5 is computed entirely at compile time using recursive template instantiation.<\/span><\/p>\n<p><b>How Template Metaprogramming Works<\/b><\/p>\n<p><span style=\"font-weight: 400;\">TMP uses recursive templates where each instantiation processes part of the computation and passes control to a specialized or base case template. This recursion unfolds during compilation, and the final computed value is made available through static members or constexpr variables.<\/span><\/p>\n<p><b>Advantages and Limitations of TMP<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Advantages:<\/b>&nbsp;\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Compile-time optimizations.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Code generation for multiple types without runtime cost.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Early error detection and stronger type safety.<\/span>&nbsp;<\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Limitations:<\/b>&nbsp;\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Complex syntax and steep learning curve.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Increased compilation times.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Difficult to debug template errors.<\/span>&nbsp;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><b>Non-Type Template Parameters<\/b><\/p>\n<p><span style=\"font-weight: 400;\">C++ templates can accept not only types but also values as template parameters. These non-type template parameters allow embedding constant values directly into templates, enabling additional flexibility for compile-time customization.<\/span><\/p>\n<p><b>Syntax for Non-Type Template Parameters<\/b><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T, int size&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class FixedArray {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0T arr[size];<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void print() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(int i = 0; i &lt; size; i++) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; arr[i] &lt;&lt; &#187; &#171;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here, <\/span><span style=\"font-weight: 400;\">size<\/span><span style=\"font-weight: 400;\"> is a non-type parameter that specifies the array length at compile time.<\/span><\/p>\n<p><b>Example: Fixed Size Array<\/b><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;iostream&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">using namespace std;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T, int size&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class FixedArray {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0T arr[size];<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0FixedArray(T initVal) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for (int i = 0; i &lt; size; i++) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0arr[i] = initVal;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void print() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for (int i = 0; i &lt; size; i++) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; arr[i] &lt;&lt; &#187; &#171;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int main() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0FixedArray&lt;int, 5&gt; intArray(10);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0intArray.print();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0FixedArray&lt;double, 3&gt; doubleArray(2.5);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0doubleArray.print();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return 0;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This example creates fixed-size arrays with initialization values set at construction.<\/span><\/p>\n<p><b>Types Allowed as Non-Type Template Parameters<\/b><\/p>\n<p><span style=\"font-weight: 400;\">C++ supports several kinds of non-type parameters, including:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Integral types (int, char, bool, etc.)<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Enumeration types<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Pointer and reference to objects with external linkage<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">nullptr (since C++11)<\/span>&nbsp;<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Note that complex types like floating-point numbers are not permitted as non-type template parameters due to a lack of guarantee on compile-time representation.<\/span><\/p>\n<p><b>Templates and the Standard Template Library (STL)<\/b><\/p>\n<p><span style=\"font-weight: 400;\">The Standard Template Library (STL) is a collection of generic classes and functions implemented extensively using C++ templates. It provides powerful data structures such as vectors, lists, maps, and algorithms that operate on these containers generically.<\/span><\/p>\n<p><b>How Templates Make STL Flexible<\/b><\/p>\n<p><span style=\"font-weight: 400;\">STL uses templates to create reusable code that works with any data type. For example, <\/span><span style=\"font-weight: 400;\">std::vector&lt;T&gt;<\/span><span style=\"font-weight: 400;\"> is a dynamic array that can hold elements of any type specified by <\/span><span style=\"font-weight: 400;\">T<\/span><span style=\"font-weight: 400;\">. Algorithms like <\/span><span style=\"font-weight: 400;\">std::sort<\/span><span style=\"font-weight: 400;\"> are also implemented as template functions that work with any container supporting iterators.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This design eliminates the need to write separate container and algorithm implementations for each data type.<\/span><\/p>\n<p><b>Example: Using std::vector and std::sort<\/b><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;iostream&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;vector&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;algorithm&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">using namespace std;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int main() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0vector&lt;int&gt; numbers = {4, 2, 5, 1, 3};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0sort(numbers.begin(), numbers.end());<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0for (int num: numbers) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; num &lt;&lt; &#187; &#171;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return 0;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The <\/span><span style=\"font-weight: 400;\">std::vector<\/span><span style=\"font-weight: 400;\"> container holds integers, and <\/span><span style=\"font-weight: 400;\">std::sort<\/span><span style=\"font-weight: 400;\"> sorts the container&#8217;s elements efficiently without any type-specific code.<\/span><\/p>\n<p><b>Benefits of STL Templates<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Reusability:<\/b><span style=\"font-weight: 400;\"> Write once, use for any data type.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Efficiency:<\/b><span style=\"font-weight: 400;\"> Optimized compile-time type-specific implementations.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Maintainability:<\/b><span style=\"font-weight: 400;\"> Reduced code duplication and simpler updates.<\/span>&nbsp;<\/li>\n<\/ul>\n<p><b>Advanced Template Concepts<\/b><\/p>\n<p><b>Variadic Templates<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Introduced in C++11, variadic templates allow templates to accept an arbitrary number of template parameters. This feature is especially useful for functions and classes that need to handle a flexible number of arguments.<\/span><\/p>\n<p><b>Syntax and Example<\/b><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;iostream&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">using namespace std;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template&lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">void print(T t) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; t &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template&lt;typename T, typename&#8230; Args&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">void print(T t, Args&#8230; args) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; t &lt;&lt; &#171;, &#171;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0print(args&#8230;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int main() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0print(1, 2.5, &#171;Hello&#187;, &#8216;a&#8217;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return 0;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here, <\/span><span style=\"font-weight: 400;\">print<\/span><span style=\"font-weight: 400;\"> can accept any number of arguments of any type, printing them separated by commas.<\/span><\/p>\n<p><b>Template Aliases<\/b><\/p>\n<p><span style=\"font-weight: 400;\">C++11 introduced <\/span><span style=\"font-weight: 400;\">using<\/span><span style=\"font-weight: 400;\"> declarations to create template aliases, simplifying complex template types.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template&lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">using Vec = std::vector&lt;T&gt;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Vec&lt;int&gt; myVec;\u00a0 \/\/ Equivalent to std::vector&lt;int&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This helps to improve code readability.<\/span><\/p>\n<p><b>Concepts (Since C++20)<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Concepts provide a way to specify constraints on template parameters, ensuring that the types used with a template meet certain requirements.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;concepts&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;iostream&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template&lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">concept Number = std::integral&lt;T&gt; || std::floating_point&lt;T&gt;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template&lt;Number T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T add(T a, T b) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return a + b;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int main() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; add(3, 4) &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0cout &lt;&lt; add(3.5, 2.5) &lt;&lt; endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\/\/ add(&#171;Hello&#187;, &#171;World&#187;);\u00a0 \/\/ Error: doesn&#8217;t satisfy Number concept<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return 0;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Concepts improve code safety and provide clearer error messages.<\/span><\/p>\n<p><b>Template Instantiation and Compilation Process<\/b><\/p>\n<p><b>How Template Instantiation Works<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Templates in C++ are not actual code until they are instantiated. Template instantiation is the process by which the compiler generates concrete code from a template definition by substituting the template parameters with specific types or values. This occurs during compilation when the compiler encounters a usage of a template with particular arguments.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example, when you call <\/span><span style=\"font-weight: 400;\">MaxVal&lt;int&gt;(5, 8)<\/span><span style=\"font-weight: 400;\">, the compiler generates a function <\/span><span style=\"font-weight: 400;\">MaxVal<\/span><span style=\"font-weight: 400;\"> specifically for the <\/span><span style=\"font-weight: 400;\">int<\/span><span style=\"font-weight: 400;\"> type based on the template definition.<\/span><\/p>\n<p><b>Types of Template Instantiation<\/b><\/p>\n<p><span style=\"font-weight: 400;\">There are two kinds of instantiation:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Implicit Instantiation:<\/b><span style=\"font-weight: 400;\"> When the compiler automatically generates code for a template upon encountering its usage with a specific type.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Explicit Instantiation:<\/b><span style=\"font-weight: 400;\"> When the programmer explicitly instructs the compiler to generate code for certain template arguments using the <\/span><span style=\"font-weight: 400;\">template<\/span><span style=\"font-weight: 400;\"> keyword.<\/span>&nbsp;<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">cpp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template class MyClass&lt;int&gt;;\u00a0 \/\/ Explicit instantiation for int<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Explicit instantiation can reduce compilation time or resolve linker errors by placing template definitions in a source file instead of a header.<\/span><\/p>\n<p><b>Template Compilation Challenges<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Templates introduce some unique compilation complexities:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Code Bloat:<\/b><span style=\"font-weight: 400;\"> Each instantiation generates new code, potentially increasing binary size if many types are used.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Longer Compilation Times:<\/b><span style=\"font-weight: 400;\"> More template usage can significantly slow down compilation due to repetitive instantiation.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Error Messages:<\/b><span style=\"font-weight: 400;\"> Template errors can be verbose and difficult to interpret, especially with deeply nested templates.<\/span>&nbsp;<\/li>\n<\/ul>\n<p><b>Template Best Practices<\/b><\/p>\n<p><b>Write a Clear and Concise Template Code<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Templates can become complicated, so strive for readability:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Use meaningful template parameter names.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Document template parameters and their expected behavior.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Keep template functions and classes focused on a single responsibility.<\/span>&nbsp;<\/li>\n<\/ul>\n<p><b>Limit Template Instantiations<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Avoid unnecessary instantiations by:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Use explicit instantiation when appropriate.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Avoid templates for types that do not benefit from generic programming.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Using type erasure or polymorphism if code bloat becomes problematic.<\/span>&nbsp;<\/li>\n<\/ul>\n<p><b>Use Concepts and Static Assertions<\/b><\/p>\n<p><span style=\"font-weight: 400;\">With C++20 concepts, enforce template constraints at compile time to improve error clarity:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">cpp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#include &lt;concepts&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">concept Number = std::integral&lt;T&gt; || std::floating_point&lt;T&gt;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;Number T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T square(T x) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return x * x;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Before concepts, use <\/span><span style=\"font-weight: 400;\">static_assert<\/span><span style=\"font-weight: 400;\"> for compile-time checks:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">cpp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">void foo() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0static_assert(std::is_integral&lt;T&gt;::value, &#171;T must be integral&#187;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\/\/ function body<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><b>Avoid Excessive Template Recursion<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Recursive templates are powerful but can lead to compile-time overhead or compiler limits. Use iterative techniques or constexpr functions when possible.<\/span><\/p>\n<p><b>Template Specialization in Depth<\/b><\/p>\n<p><b>What is Template Specialization?<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Template specialization allows customizing the implementation of a template for a particular type or set of types. This is useful when the generic template implementation is not efficient or appropriate for certain data types.<\/span><\/p>\n<p><b>Full Specialization<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Full specialization means providing an entirely separate definition for specific template arguments.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">cpp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">struct Printer {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void print() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; &#171;Generic template&#187; &lt;&lt; std::endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">struct Printer&lt;int&gt; {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void print() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; &#171;Specialized template for int&#187; &lt;&lt; std::endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><b>Partial Specialization<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Partial specialization modifies behavior for a subset of template parameters.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">cpp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T, typename U&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">struct Pair {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void info() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; &#171;Generic Pair&#187; &lt;&lt; std::endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">struct Pair&lt;T, int&gt; {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void info() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; &#171;Pair with second type int&#187; &lt;&lt; std::endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Partial specialization cannot be applied to function templates but is allowed for class templates.<\/span><\/p>\n<p><b>Use Cases for Specialization<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Optimizing for specific types.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Providing better error messages.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Handling types that require different operations.<\/span>&nbsp;<\/li>\n<\/ul>\n<p><b>Templates with Inheritance and Polymorphism<\/b><\/p>\n<p><b>Combining Templates with Inheritance<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Templates can be used as base or derived classes, enabling generic polymorphic behaviors.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">cpp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Base {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void print() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; &#171;Base class: &#187; &lt;&lt; typeid(T).name() &lt;&lt; std::endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Derived: public Base&lt;int&gt; {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><b>Curiously Recurring Template Pattern (CRTP)<\/b><\/p>\n<p><span style=\"font-weight: 400;\">CRTP is a technique where a class template takes a derived class as a template parameter to achieve static polymorphism.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">cpp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename Derived&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Base {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void interface() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0static_cast&lt;Derived*&gt;(this)-&gt;implementation();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Derived public Base&lt;Derived&gt; {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void implementation() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; &#171;Derived implementation&#187; &lt;&lt; std::endl;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int main() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Derived d;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0d.interface();\u00a0 \/\/ Calls Derived::implementation<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><b>Advantages of CRTP<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Avoids runtime overhead of virtual functions.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Enables compile-time polymorphism.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Facilitates code reuse and static dispatch.<\/span>&nbsp;<\/li>\n<\/ul>\n<p><b>Templates and Exceptions<\/b><\/p>\n<p><b>Exception Handling in Templates<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Templates can throw exceptions just like normal functions and classes. However, care must be taken because exception specifications are not always compatible with all template instantiations.<\/span><\/p>\n<p><b>Exception Safety in Template Code<\/b><\/p>\n<p><span style=\"font-weight: 400;\">When writing template functions or classes, ensure exception safety by:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Using RAII (Resource Acquisition Is Initialization) for managing resources.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Avoid throwing exceptions in destructors.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Testing template code with various types to verify exception behavior.<\/span>&nbsp;<\/li>\n<\/ul>\n<p><b>Practical Examples of Templates<\/b><\/p>\n<p><b>Generic Stack Implementation<\/b><\/p>\n<p><span style=\"font-weight: 400;\">cpp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Stack {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">private:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0T* arr;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0int capacity;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0int top;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Stack(int size) : capacity(size), top(-1) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0arr = new T[capacity];<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0~Stack() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0delete[] arr;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void push(const T&amp; val) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (top &lt; capacity &#8212; 1)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0arr[++top] = val;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0T pop() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (top &gt;= 0)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return arr[top&#8212;];<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0throw std::out_of_range(&#171;Stack underflow&#187;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0bool isEmpty() const {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return top == -1;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This example demonstrates a generic stack that works with any data type.<\/span><\/p>\n<p><b>Generic Linked List<\/b><\/p>\n<p><span style=\"font-weight: 400;\">cpp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Node {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0T data;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Node* next;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Node(T val) : data(val), next(nullptr) {}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">template &lt;typename T&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class LinkedList {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">private:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Node&lt;T&gt;* head;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0LinkedList() : head(nullptr) {}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void insert(T val) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Node&lt;T&gt;* newNode = new Node&lt;T&gt;(val);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0newNode-&gt;next = head;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0head = newNode;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0void print() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Node&lt;T&gt;* temp = head;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0while (temp != nullptr) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; temp-&gt;data &lt;&lt; &#187; -&gt; &#171;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0temp = temp-&gt;next;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; &#171;nullptr\\n&#187;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">};<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The linked list template can store any data type and perform insertions and traversals.<\/span><\/p>\n<p><b>Common Template Errors and Debugging Tips<\/b><\/p>\n<p><b>Common Errors<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Undefined reference to template functions:<\/b><span style=\"font-weight: 400;\"> Usually due to template definitions being in source files instead of headers.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Mismatched template parameters:<\/b><span style=\"font-weight: 400;\"> Wrong number or type of template arguments.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Deep template recursion limit exceeded:<\/b><span style=\"font-weight: 400;\"> Too many recursive template instantiations.<\/span>&nbsp;<\/li>\n<\/ul>\n<p><b>Debugging Tips<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Move template definitions to header files.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Use explicit instantiation to control code generation.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Simplify templates to isolate errors.<\/span>&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Use compiler options for more detailed template error messages.<\/span>&nbsp;<\/li>\n<\/ul>\n<p><b>Conclusion<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Templates are a cornerstone of modern C++ programming, providing immense power to write flexible, reusable, and efficient code. Understanding their syntax, behavior, and advanced features like specialization, metaprogramming, and concepts enables developers to build robust and optimized applications.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Mastering templates involves continuous learning and experimentation, especially with new standards that enhance their capabilities. By following best practices and learning from practical examples, you can harness the full potential of templates in your C++ projects.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Templates in C++ serve as the cornerstone of generic programming. They allow programmers to create functions and classes that operate with any data type. Rather than duplicating code for each data type, templates offer a single definition that works with various types. This significantly reduces code redundancy and enhances code maintainability. Templates enable a developer to write more flexible and reusable code. When a template is instantiated, the compiler generates the appropriate function or class by replacing the generic type with the actual [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1049,1053],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/posts\/951"}],"collection":[{"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/comments?post=951"}],"version-history":[{"count":2,"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/posts\/951\/revisions"}],"predecessor-version":[{"id":9784,"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/posts\/951\/revisions\/9784"}],"wp:attachment":[{"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/media?parent=951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/categories?post=951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/tags?post=951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}