{"id":25674,"date":"2023-11-08T10:00:20","date_gmt":"2023-11-08T04:30:20","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/"},"modified":"2025-01-06T20:06:05","modified_gmt":"2025-01-06T14:36:05","slug":"templates-in-cpp","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/","title":{"rendered":"Templates in C++ With Examples"},"content":{"rendered":"\n<p>Templates in c++ is defined as a blueprint or formula for creating a generic class or a function. Generic Programming is an approach to programming where generic types are used as parameters in algorithms to work for a variety of data types.In C++, a template is a straightforward yet effective tool. To avoid having to write the same code for many data types, the simple concept is to pass the data type as a parameter.&nbsp;<\/p>\n\n\n\n<p>Special functions that can interact with generic types are known as function templates. As a result, we can develop a function template whose functionality can be applied to multiple types or classes without having to duplicate the full code for each kind.<\/p>\n\n\n\n<p>This may be done in C++ by using template parameters. Similar to how standard function parameters may be used to send values to a function, template parameters allow you to pass types to a function as well. A template parameter is a specific form of the parameter that can be used to pass a type as an argument. These parameters can be used by these function templates just like any other ordinary type.<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-for-beginners-to-advanced\" class=\"courses-cta-title-link\">C++ Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Master key C++ programming concepts like variables, functions, OOP, and control structures. Build real-world projects such as a banking system and grade management tool.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>Beginner to Advanced Level<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>8.1 hrs<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-for-beginners-to-advanced\" class=\"courses-cta-button\">\n                Start Free Trial\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<p>So, this blog is here to help you out in understanding the details of templates in c++, how they work, templates specialization, templates library and many more. Read on to know more!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"table-of-contents\"><strong>Table of Contents&nbsp;<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><a href=\"# What is Templates in C++?\" data-type=\"internal\">What is Templates in C++?<\/a><\/strong> <\/li>\n\n\n\n<li><a href=\"#How do templates work in c++?\"><strong>How do templates work in C++?<\/strong>  <\/a> <\/li>\n\n\n\n<li><strong><a href=\"#Types of Templates\">Types of Templates in C++<\/a><\/strong>\n<ul class=\"wp-block-list\">\n<li><a href=\"#What is the function template in C++?\"><strong><strong>What is the function template in C++?<\/strong><\/strong><\/a><\/li>\n\n\n\n<li><a href=\"#What is class template in C++?\"><strong>What is class template in c++<\/strong>?<\/a><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><a href=\"#Difference between function overloading and templates in C++?\">Difference between function overloading and templates in C+<\/a><a href=\"#What is the difference between function overloading and templates in C++?\">+?<\/a><\/strong><\/li>\n\n\n\n<li><strong><a href=\"#Advantages of Using Templates\">Advantages of Using Templates in C++<\/a><\/strong><\/li>\n\n\n\n<li><strong><a href=\"#Disadvantages of Using Templates\">Disadvantages of Using Templates in C++<\/a><\/strong><\/li>\n\n\n\n<li><strong><a href=\"#Overloading of Template Function\">Overloading of Template Function<\/a> <\/strong><\/li>\n\n\n\n<li><strong><a href=\"#What is Templates Specialization?\">What is Templates Specialization?&nbsp;<\/a><\/strong><\/li>\n\n\n\n<li><strong><a href=\"#Function Specialization\">Function Specialization<\/a><\/strong><\/li>\n\n\n\n<li><strong><a href=\"#Class Specialization\">Class Specialization<\/a><\/strong><\/li>\n\n\n\n<li><strong><a href=\"#Template Parameter\">Template Parameter<\/a><\/strong>\n<ul class=\"wp-block-list\">\n<li><a href=\"#Can we pass non-type parameters to templates?\"><strong>Can we pass non-type parameters to templates?<\/strong><\/a><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><a href=\"#12\" data-type=\"internal\" data-id=\"#12\"><strong>Standard Template Library in C++<\/strong><\/a><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-templates-in-c\"><strong>What is Templates in C++?<\/strong><\/h2>\n\n\n\n<p>Templates in C++ is an interesting feature that is used for generic programming and templates in c++ is defined as a blueprint or formula for creating a generic class or a function. Simply put, you can create a single function or single class to work with different data types using templates.&nbsp;<\/p>\n\n\n\n<p>C++ template is also known as generic functions or classes which is a very powerful feature in C++. A keyword \u201ctemplate\u201d in c++ is used for the template\u2019s syntax and angled bracket in a parameter (t), which defines the data type variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-do-templates-work-in-c\"><strong>How do templates work in C++?<\/strong><\/h2>\n\n\n\n<p>Templates in c++ works in such a way that it gets expanded at compiler time, just like macros and allows a function or class to work on different data types without being rewritten.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"types-of-templates-in-c\"><strong>Types of Templates in C++<\/strong><\/h2>\n\n\n\n<p>There are two types of templates in C++<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Function template<\/strong> <\/li>\n\n\n\n<li><strong>Class templates <\/strong><\/li>\n<\/ul>\n\n\n<figure class=\"wp-block-image size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/image-8.png\"><img decoding=\"async\" width=\"1024\" height=\"337\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/image-8-1024x337.png\" alt=\"Types of Templates in C++\" class=\"wp-image-81794\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/image-8-1024x337.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/image-8-300x99.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/image-8-768x253.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/image-8-696x229.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/image-8-1068x351.png 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/image-8-150x49.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/image-8.png 1177w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-function-template-in-c\"><strong>What is the function template in C++?<\/strong><\/h2>\n\n\n\n<p>A function template in c++ is a single function template that works with multiple data types simultaneously, but a standard function works only with one set of data types.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"c-function-template-syntax\"><strong>C++ Function Template Syntax<\/strong><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\ntemplate&lt;class type&gt;ret-type func-name(parameter list)\n{\n\/\/body of the function\n}\n<\/pre><\/div>\n\n\n<p>Here, type is a placeholder name for a data type used by the function. It is used within the function definition.<\/p>\n\n\n\n<p>The class keyword is used to specify a generic type in a template declaration.<\/p>\n\n\n\n<p><strong>C++ function template example:<\/strong><\/p>\n\n\n\n<p><strong>Source Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>#include&lt;iostream.h&gt;\nusing namespace std;\ntemplate&lt;classX&gt;\/\/can replace 'class\" keyword by \"typename\" keyword\nX func( Xa,Xb)\n{\nreturn a;\n}\nint main()\ncount&lt;&lt;func(15,8),,endl;\/\/func(int,int);\ncount,,func('p','q'),,endl;\/\/func(char,char);\ncount&lt;&lt;func(7.5,9.2),,endl;\/\/func(double,double)\nreturn();\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>Output \n15\np\n7.5<\/code><\/pre>\n\n\n\n<p><strong>Defining a Class Member Outside the Class Template<\/strong><\/p>\n\n\n\n<p>You\u2019ve seen how a member function is defined within a class template. Now, alternatively, it is also possible for the template member function to be defined independently of the declaration of its class template.<\/p>\n\n\n\n<p>The compiler will use the template arguments that you used, in this case, to create the class template when you call a member function of a class template specialization. The following illustration exemplifies this:<\/p>\n\n\n\n<p><strong><em>Example:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>#include&lt;iostream&gt;\nusing namespace std;\n    template&lt;class A&gt; class B {\n   public:\n      A operator+(A);\n};\n\ntemplate&lt;class A&gt; A B&lt;A&gt;::operator+(A x) {\n   return x;\n};\n\nint main() {\n   B&lt;char&gt; ob1;\n   B&lt;int&gt; ob2;\n   cout&lt;&lt; ob1 +'p' &lt;&lt; endl;\n   cout&lt;&lt; ob2 + 10;\n   return 0;\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>Output:\nP\n10<\/code><\/pre>\n\n\n\n<p>A definition of the overloaded addition operator is given outside of class A. It is equal to the statement ob1.operator+('p') rather than ob1+ \"p\". \"ob2 + 10\" is similar to \"ob2.operator+\" (10).<\/p>\n\n\n\n<p><strong>Simple Calculator Using Class Templates<\/strong><\/p>\n\n\n\n<p>We can make a simple Calculator performing the four basic arithmetic operations in C++ using a class template. The template of the class will consist of two variables whose values are passed at the time of object creation. The constructor of this class takes two arguments of generic datatypes. Further, you will see, this Calculator class template consists of five main functions \u2013 show(), addition(), subtraction(), multiplication(), and division().&nbsp; The show() function is responsible for calling the rest of the four generic functions. Moving ahead, we have created two instances from the template of Calculator class and performed the basic calculations using its class functions.&nbsp;<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>template &lt;class Temp&gt;\nclass Calculator\n{\nprivate:\n        Temp n1, n2;\n        \npublic:\n        Calculator(Temp num1, Temp num2)\n        {\n                n1 = num1;\n                n2 = num2;\n        }\n        \n        void show()\n        {\n                cout &lt;&lt; \"Addition is: \" &lt;&lt; n1 &lt;&lt; \"+\" &lt;&lt; n2 &lt;&lt; \"=\" &lt;&lt; addition() &lt;&lt; endl;\n                cout &lt;&lt; \"Subtraction is: \" &lt;&lt;n1 &lt;&lt; \"-\" &lt;&lt; n2 &lt;&lt; \"=\" &lt;&lt; subtraction() &lt;&lt; endl;\n                cout &lt;&lt; \"Product is: \" &lt;&lt; n1 &lt;&lt; \"*\" &lt;&lt; n2 &lt;&lt; \"=\" &lt;&lt; multiplication() &lt;&lt; endl;\n                cout &lt;&lt; \"Division is: \" &lt;&lt; n1 &lt;&lt; \"\/\" &lt;&lt; n2 &lt;&lt; \"=\" &lt;&lt; division() &lt;&lt; endl;\n        }\n        \n        Temp addition() { return (n1 + n2); }\n        \n        Temp subtraction() { return n1 - n2; }\n        \n        Temp multiplication() { return n1 * n2; }\n        \n        Temp division() { return n1 \/ n2; }\n};\n\nint main()\n{\n Calculator&lt;int&gt; Calc1(25, 12);\n        Calculator&lt;float&gt; Calc2(13.6, 5.9);\n        \n        cout &lt;&lt; \"Integer results for 25 and 12:\" &lt;&lt; endl;\n        Calc1.show();\n        \n        cout &lt;&lt; endl &lt;&lt; \"Float results for 13.6 and 5.9:\" &lt;&lt; endl;\n        Calc2.show();\n        \n        return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>Output <\/p>\n\n\n\n<p><img decoding=\"async\" width=\"283\" height=\"254\" src=\"https:\/\/lh5.googleusercontent.com\/Q6di86R-9-Tm647UPDfio4bxa9uZ4Zo77MMcWJfO2hT4u-mK-Hro3o9Jw19c2pXnbWK0QZjEVQPsd_D3FxvJO5Zht4KmOoMdq2p80JB26WrYhM7Td4pMiYgW1deaAlUT3kCnBwMcrmwVHSichrBjXcz6liKy3iwSTMsMtvPPbqjOYOXcCU3Mn2lu45l2gOiEfAGPKw\"><\/p>\n\n\n\n<p><strong>C++ Class Templates With Multiple Parameters<\/strong><\/p>\n\n\n\n<p>&nbsp;It is possible to provide more than one type when generating templates. A class template can have many generic data types. They are listed in a comma-delimited format as shown in the following example.<\/p>\n\n\n\n<p><strong><em>Example:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>#include&lt;iostream&gt;\nusing namespace std;\n\n\/\/ Class template with more than one parameter\ntemplate&lt;class Temp1, class Temp2&gt;\nclass Sample\n{\n\t\tTemp1 l;\n\t\tTemp2 m;\n\tpublic:\n\t\tSample(Temp1 a, Temp2 b)\n\t\t{\n\t\t\tl = a;\n\t\t\tm = b;\n\t\t}\n\t\tvoid display()\n\t\t{\n\t\t\tcout &lt;&lt; \"The inputs are: \" &lt;&lt;l &lt;&lt; \" and \" &lt;&lt; m &lt;&lt; endl;\n\t\t}\n};\n\n\/\/ Driver function\nint main()\n{\n\t\/\/ instantiation\n\tSample &lt;int, char&gt; ob1 (24, 'A');\n\t\n\t\/\/ instantiation \n\tSample&lt;int, float&gt; ob2(22.56, 34.9);\n\t\n\t\/\/Calling the display function to see the output\n             ob1.display();\n\tob2.display();\n\t\n\treturn 0;\n}\n<\/code><\/pre>\n\n\n\n<p><strong><em>Output:<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/KZUbf4HO4px5am34ew_2-f6THDGqlK5GtwrR4yaX4Mqalzlh1BqsiqHK-NGRDRFLyGLqsUB8hYA4zSb-_BneQqFCgE5ws5tQByVSfQ7gvzklrQZkQnNVISsiN80mLFjldVuVwsZF5iBE0R7gKDC7td67KdZwwt7KcMExOc9Y4WTry1kSHU_qi4ai1A70Tl_SItGWfA\" alt=\"\"\/><figcaption class=\"wp-element-caption\"><br><\/figcaption><\/figure>\n\n\n\n<p>As seen in the above cited example, we can use more than one parameter for a template in C++. Here, the template Temp takes two parameters, both of the Sample class type, separated by a comma. Moreover, the constructor of the Sample class takes two arguments of generic datatype. When we create objects, the types of arguments are specified in angular brackets (&lt; &gt;). In case of multiple parameters, the datatypes are also separated by commas. The constructor is called at the time when objects are created, and template arguments receive values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-class-template-in-c\"><strong>What is class template in c++<\/strong>?<\/h2>\n\n\n\n<p>The class template in c++ is like function templates. They are known as generic templates. They define a family of classes in C++.&nbsp;<\/p>\n\n\n\n<p><strong>Syntax of Class Template<\/strong> <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\ntemplate&lt;class Ttype&gt;\nclass class_name\n{\n\/\/class body;\n}\n<\/pre><\/div>\n\n\n<p>Here Type is a placeholder type name, which will be specified when a class instantiated.<\/p>\n\n\n\n<p>The Type can be used inside the body of the class.&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p><strong>Class template in c++ example:<\/strong><\/p>\n\n\n\n<p><strong>Source Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>#include&lt;iostream.h&gt;\nusing namespace std;\ntemplate &lt;class C&gt;\nclass A{\nprivate;\nC,a,b;\npublic:\nA(Cx,Cy){\na=x;\nb=y;\n}\nvoid show()\n}\ncount&lt;&lt;\"The Addition of\"&lt;&lt;a&lt;&lt;\"and\"&lt;&lt;b&lt;&lt;\"is\"&lt;&lt;add()&lt;&lt;endl;\n}\nC add(){\nC c=a+b;\nreturn c;\n}\n};\nint main(){\nAaddint(8,6);\nAaddfloat(3.5,2.6);\nAaaddouble(2.156,5.234);\nAaddint.show();\ncout&lt;&lt;endl;\nadddouble.show();\ncount&lt;&lt;endl;\nreturn 0;\n}\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>Output\nThe addition of 8 and 6 is 14 \nAddition of 3.5 and 2.6 is 6.1\nThe addition of 2.156 and 5.234 is 7.390<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"difference-between-function-overloading-and-templates-in-c\"><strong>Difference between function overloading and templates in C++?<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><strong>Function overloading<\/strong><\/td><td><strong>Function Template<\/strong><\/td><\/tr><tr><td>This is used when multiple functions do similar operations.<\/td><td>This is used when functions do identical operations.<\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/blog\/function-overloading-in-cpp\/\" target=\"_blank\" rel=\"noreferrer noopener\">Function overloading<\/a> can take varying numbers of arguments.<\/td><td>Templates cannot take varying numbers of arguments.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advantages-of-using-templates-in-c\"><strong>Advantages of Using Templates in C++<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Templates are type-safe.<\/li>\n\n\n\n<li>They are generally considered as an improvement over macros for these purposes.<\/li>\n\n\n\n<li>Templates avoid some common errors found in code that make heavy use of function-like macros.<\/li>\n\n\n\n<li>Both templates and macros are expanded at compile time.<\/li>\n\n\n\n<li>They are a good way of making generalisations for APIs.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"disadvantages-of-using-templates-in-c\"><strong>Disadvantages of Using Templates in C++<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Many compilers do not support nesting of templates.<\/li>\n\n\n\n<li>When templates are used, all codes exposed.<\/li>\n\n\n\n<li>&nbsp;Some compilers have poor support of templates.<\/li>\n\n\n\n<li>Approx all compilers produce unhelpful, confusing error messages when errors are detected in the template code.<\/li>\n\n\n\n<li>It can make it challenging to develop the template.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"overloading-of-c-template\"><strong>Overloading of  C++ template<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A template function is called that could be created with an exact match.<\/li>\n\n\n\n<li>Call an ordinary function that has an exact match.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-templates-specialization\"><strong>What is Templates Specialization?&nbsp;<\/strong><\/h2>\n\n\n\n<p>Templates Specialization is defined as a mechanism that allows any programmer to use types as parameters for a class or a function. &nbsp;A function\/class defined using the template is called a&nbsp;generic function\/class, and the ability to use and create generic functions\/classes is one of the critical features of C++.<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>\/\/A generic sort function\ntemplate&lt;class X&gt;\n{\n\/\/code to implement quick sort\n}\n\/\/Template specilization:A function\n\/\/specialized for char data type\ntemplate&lt;&gt;\nvoid sort&lt;char&gt;(char arr&#091;],int size)\n{\n\/\/code to impletement counting sort\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"function-specialization\"><strong>Function Specialization<\/strong><\/h2>\n\n\n\n<p>We have the add() function, which takes two parameters and returns the same type of data after adding the two args.<\/p>\n\n\n\n<p><strong>Function <strong>specialization<\/strong> example: &nbsp;<\/strong><\/p>\n\n\n\n<p><strong>Source Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>#include &lt;iostream.h&gt;\nusing namespace std ;\n\n\/\/max returns the maximum of the two elements of type T, where T is a\n\/\/class or data type for which operator&gt; is defined.\ntemplate &lt;class T&gt;\nT max(T a, T b)\n{\n    return a &gt; b ? a : b ;\n}\n\nint main()\n{    \n    cout &lt;&lt; \"max(80, 95) = \" &lt;&lt; max(10, 15) &lt;&lt; endl ;\n    cout &lt;&lt; \"max('a', 'z') = \" &lt;&lt; max('k', 's') &lt;&lt; endl ;\n    cout &lt;&lt; \"max(11.1, 18.2) = \" &lt;&lt; max(11.1, 18.2) &lt;&lt; endl ;\n    cout &lt;&lt; \"max(\"Ahil\", \"Riya\") = \" &lt;&lt; max(\"Ahil\", \"Riya\") &lt;&lt; endl ;\n    return 0 ;\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>Output\nmax(80, 95) = 95\nmax('a', 'z') = z\nmax(11.1, 18.2) = 18.2\nmax(\"Ahil\", \"Riya\") = Riya\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"class-specialization\"><strong>&nbsp;Class Specialization<\/strong><\/h2>\n\n\n\n<p><strong>Class <strong>specialization<\/strong> example: &nbsp;&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>Source Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>#include &lt;iostream&gt; \nusing namespace std; \n  \ntemplate &lt;class T&gt; \nclass Test \n{ \n  \/\/ Data memnbers of test \npublic: \n   Test() \n   { \n       \/\/ Initialization of data members \n       cout &lt;&lt; \"General template  n\"; \n   } \n   \/\/ Other methods of Test \n}; \n  \ntemplate &lt;&gt; \nclass Test &lt;int&gt; \n{ \npublic: \n   Test() \n   { \n       \/\/ Initialization of data members \n       cout &lt;&lt; \"Specialized template n\"; \n   } \n}; \n  \nint main() \n{ \n    Test&lt;int&gt; a; \n    Test&lt;char&gt; b; \n    Test&lt;float&gt; c; \n    return 0; \n} <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>Output\nSpecialized template  \nGeneral template \nGeneral template \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-does-template-specialisation-work\"><strong>How does template specialisation work?<\/strong><\/h2>\n\n\n\n<p>When we write any template-based function or class, the compiler creates a copy of that function\/class whenever the compiler sees that being used for a new data type or a new set of data types(in case of multiple template arguments).<br>If a specialised version is present, the compiler first checks with the specialised version and then the main template. The compiler first checks with the most specialised version by matching the passed parameter with the data type(s) specified in a specialised version.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"template-parameter\"><strong>Template Parameter<\/strong><\/h2>\n\n\n\n<p>Template parameters can be types, non-types, and templates.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Types:<\/strong> Types are the most often used template parameters.<\/li>\n\n\n\n<li><strong>Non-Types:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>lvalue reference<\/li>\n\n\n\n<li>nullptr<\/li>\n\n\n\n<li>pointer<\/li>\n\n\n\n<li>enumerator<\/li>\n\n\n\n<li>integral<\/li>\n<\/ul>\n\n\n\n<p>Integrals are the most used non-types.&nbsp;std::array&nbsp;is the typical example because you have to specify at compile time the size of a&nbsp;std::array:<\/p>\n\n\n\n<p>Std::array&lt;int, 3&gt;&nbsp; myarray{1, 2, 3};<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"can-we-pass-non-type-parameters-to-templates\"><strong>Can we pass non-type parameters to templates?<\/strong><\/h2>\n\n\n\n<p>The non-type parameters can be passed to templates. They mainly focus on passing the constant expression i.e address of a function, object or static class member at compile time. Non-type parameters are also used for specifying max or min values for a particular instance of a template.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"source-code\"><strong>Source Code<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>template &lt;std::string * temp&gt; \/\/pointer to object\nvoid f()\n{\n   cout &lt;&lt; *temp &lt;&lt; endl;\n}\n\ntemplate &lt;std::string &amp; temp&gt; \/\/reference to object\nvoid g()\n{\n     cout &lt;&lt; temp &lt;&lt; endl;\n     temp += \"...appended some string\";\n}\n\nstd::string s; \/\/must not be local as it must have external linkage!\n\nint main() {\n        s = \"can assign values locally\";\n        f&lt;&amp;s&gt;();\n        g&lt;s&gt;();\n        cout &lt;&lt; s &lt;&lt; endl;\n        return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"output\"><strong>Output:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code><strong>Output\n<\/strong>Can assign values locally\nAssign values locally\nWe can assign values locally...appended some string\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create-multi-file-templates\"><strong>Create Multi-File Templates<\/strong><\/h2>\n\n\n\n<p>Item templates may only specify one item, but sometimes the item is made up of multiple files.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A file that contains code for the form<\/li>\n\n\n\n<li>The file that contains designer information for the form<\/li>\n\n\n\n<li>A file that contains embedded resources for the form<\/li>\n<\/ul>\n\n\n\n<p>The set of&nbsp;parameters&nbsp;spans all files of a multi-file template. If you use the same parameter&nbsp;$PARAM$&nbsp;in several files of a multi-file template, it will have the same value in all related files when the template is applied.<\/p>\n\n\n\n<p>If a multi-file template has&nbsp;editable parameters, the&nbsp;Hot Spot Session, which deploys when you apply this template, will guide you through all created files where the user input is required.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"manually-create-a-multi-file-item-template\"><strong>Manually create a multi-file item template.<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create an item template as you would manually create a single-file item template, but include each of the files that constitute the multi-file item.<\/li>\n\n\n\n<li>In the '.vstemplate' XML file, add a 'ProjectItem' element for each individual file, and add a 'TargetFileName' attribute to this element. Set value of the TargetFileName attribute to '$fileinputname$.FileExtension', where FileExtension is the file extension of the file that is included in the template.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p><strong>&nbsp;Multi-file item template example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;ProjectItem TargetFileName=&quot;$fileinputname$.vb&quot;&gt;\n    Form1.vb\n&amp;lt;\/ProjectItem&gt;\n&amp;lt;ProjectItem TargetFileName=&quot;$fileinputname$.Designer.vb&quot;&gt;\n    Form1.Designer.vb\n&amp;lt;\/ProjectItem&gt;\n&amp;lt;ProjectItem TargetFileName=&quot;$fileinputname$.resx&quot;&gt;\n    Form1.resx\n&amp;lt;\/ProjectItem&gt;\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>Select the files that need to be included in your template, right-click the selection, and choose <strong>Send to<\/strong> &gt; <strong>Compressed (zipped) folder<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p>The files that you have selected are compressed into a .zip file.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Copy the .zip file to the user-item template location. The directory is '%USERPROFILE%DocumentsVisual Studio&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>&lt;Version&gt;TemplatesItemTemplates' by default.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Close Visual Studio and then open it.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a new project, or open an already existing project, and then select <strong>Project<\/strong> &gt; <strong>Add New Item<\/strong> or press <strong>Ctrl<\/strong>+<strong>Shift<\/strong>+<strong>A<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p>The multi-file item template appears in the '<strong>Add New Item'<\/strong> dialogue box.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"example\"><strong>Example<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>&lt;VSTemplate Version=\"2.0.0\" Type=\"Item\"\n    xmlns=\"http:\/\/schemas.microsoft.com\/developer\/vstemplate\/2005\"&gt;\n    &lt;TemplateData&gt;\n        &lt;Name&gt;Multi-file Item Template&lt;\/Name&gt;\n        &lt;Icon&gt;Icon.ico&lt;\/Icon&gt;\n        &lt;Description&gt;An example of a multi-file item template&lt;\/Description&gt;\n        &lt;ProjectType&gt;VisualBasic&lt;\/ProjectType&gt;\n    &lt;\/TemplateData&gt;\n    &lt;TemplateContent&gt;\n        &lt;ProjectItem TargetFileName=\"$fileinputname$.vb\" SubType=\"Form\"&gt;\n            Form1.vb\n        &lt;\/ProjectItem&gt;\n        &lt;ProjectItem TargetFileName=\"$fileinputname$.Designer.vb\"&gt;\n            Form1.Designer.vb\n        &lt;\/ProjectItem&gt;\n        &lt;ProjectItem TargetFileName=\"$fileinputname$.resx\"&gt;\n            Form1.resx\n        &lt;\/ProjectItem&gt;\n    &lt;\/TemplateContent&gt;\n&lt;\/VSTemplate&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"standard-template-library-in-c\"><strong>Standard Template Library in C++<\/strong><\/h2>\n\n\n\n<p>A standard template library in C++ is a repository of C++ template classes. These template classes help in implementing commonly used algorithms and data structures. These template classes can be used with any user-defined type as well as a built-in type. STL algorithms are container-independent and thus reduce the complexity of the library. This approach implements compile-time polymorphism over run-time polymorphism. <\/p>\n\n\n\n<p>This brings us to the end of the blog on Templates in C++. Hope this helps you to up-skill your C++ skills. To learn more about programming and other related concepts, check out the courses on&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.mygreatlearning.com\/academy\/#our-courses\" target=\"_blank\">Great Learning Academy<\/a>.&nbsp;<\/p>\n\n\n\n<p>Also, if you are preparing for Interviews, check out these&nbsp;<a href=\"https:\/\/www.mygreatlearning.com\/blog\/cpp-interview-questions\/\" target=\"_blank\" rel=\"noreferrer noopener\">Interview Questions for C++<\/a>&nbsp;to ace it like a pro.<\/p>\n\n\n\n<p>Template in C++ FAQs<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1663562642972\"><strong class=\"schema-faq-question\"><strong>What are CPP templates?<\/strong><\/strong> <p class=\"schema-faq-answer\">In C++, a template is referred to be a design pattern or recipe for developing a generic class or function. Simply put, you may use templates to construct a single function or single class that works with several data types. A very useful element of C++ is the generic functions or classes, sometimes referred to as C++ templates. The data type variable is defined by an angled bracket in a parameter (t) and the keyword \"template\" in the C++ programming language.<br \/><\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1663562656622\"><strong class=\"schema-faq-question\"><strong>What types of templates are there in CPP?<\/strong><br \/><\/strong> <p class=\"schema-faq-answer\">Function templates, class templates, and, as of C++14, variable templates are the three categories of templates. Templates are always non-variadic in older versions of C++. However, the templates in C++ can either be variadic or non-variadic.\u00a0<br \/><\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1663562676535\"><strong class=\"schema-faq-question\"><strong>How many templates are there in CPP?<\/strong><br \/><\/strong> <p class=\"schema-faq-answer\">As of the latest version, CPP14, there are three main templates, namely function, class , and variable templates.<br \/><\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1663562689465\"><strong class=\"schema-faq-question\"><strong>Why do we use templates in CPP?<\/strong><br \/><\/strong> <p class=\"schema-faq-answer\">When creating generic constructs like queues, lists, stacks, and vectors that can be used with any type, templates come in quite handy. In contrast to inheritance and composition, which enable the reuse of object code, C++ templates enable the reuse of source code.<br \/><\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1663562699360\"><strong class=\"schema-faq-question\"><strong>What are the different types of templates?<\/strong><br \/><\/strong> <p class=\"schema-faq-answer\">Different types of templates supported by C++14 are, class templates, function templates, vardiac, and non-vardiac templates.\u00a0<br \/><\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1663562717768\"><strong class=\"schema-faq-question\"><strong>What is the use of templates?<\/strong><br \/><\/strong> <p class=\"schema-faq-answer\">In general, an instrument for imposing a consistent layout, appearance, and feel across several pages or inside content sections is a template. Any pages or regions that are based on a template are immediately modified whenever a template is altered. This concept is applied in codes as well, so that we can use a defined layout multiple times.\u00a0<br \/><\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1663562732343\"><strong class=\"schema-faq-question\"><strong>What are two types of templates?<\/strong><br \/><\/strong> <p class=\"schema-faq-answer\">The two major categories of templates are \u2013 class templates and function templates.<br \/><\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1663562748443\"><strong class=\"schema-faq-question\"><strong>What is a template example?<\/strong><br \/><\/strong> <p class=\"schema-faq-answer\">A class or a function whose layout can be used as many times as required in a code is made into a template. For example, we can create templates for functions like sort(), minimum(), maximum(), arrayPrint(), Fibonacci(), etc. Similarly, we can create class templates for heavy structures, for instance, linkedList(), Queue(), Stack(), Book(), etc.\u00a0<br \/><\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1663562757475\"><strong class=\"schema-faq-question\"><strong>What is a template and what are its advantages?<\/strong><br \/><\/strong> <p class=\"schema-faq-answer\">With the aid of C++ templates, you can create a group of classes or functions that can handle various forms of data. When there is a need to duplicate the same code across many types, we use templates. Several advantages of templates are as follows:<br \/>They increase the efficiency of the program by reducing the developing-time when used in combination with STL<br \/>They permit type generalization.<br \/>They reduce the quantity of repetitive code you must type.<br \/>They assist in writing type-safe code.<br \/>They are assessed during compilation.<br \/>They aid in creating extremely powerful libraries<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Templates in c++ is defined as a blueprint or formula for creating a generic class or a function. Generic Programming is an approach to programming where generic types are used as parameters in algorithms to work for a variety of data types.In C++, a template is a straightforward yet effective tool. To avoid having to [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":81862,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[],"content_type":[],"class_list":["post-25674","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Templates in C++<\/title>\n<meta name=\"description\" content=\"A template in c++ is defined as a blueprint for creating a generic class or a function. Templates are a very powerful feature of C++.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Templates in C++ With Examples\" \/>\n<meta property=\"og:description\" content=\"A template in c++ is defined as a blueprint for creating a generic class or a function. Templates are a very powerful feature of C++.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-08T04:30:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-06T14:36:05+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Templates in C++ With Examples\",\"datePublished\":\"2023-11-08T04:30:20+00:00\",\"dateModified\":\"2025-01-06T14:36:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/\"},\"wordCount\":2371,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/Blog-Featured-Image-4.jpg\",\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/\",\"name\":\"Templates in C++\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/Blog-Featured-Image-4.jpg\",\"datePublished\":\"2023-11-08T04:30:20+00:00\",\"dateModified\":\"2025-01-06T14:36:05+00:00\",\"description\":\"A template in c++ is defined as a blueprint for creating a generic class or a function. Templates are a very powerful feature of C++.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562642972\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562656622\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562676535\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562689465\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562699360\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562717768\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562732343\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562748443\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562757475\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/Blog-Featured-Image-4.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/Blog-Featured-Image-4.jpg\",\"width\":1200,\"height\":628,\"caption\":\"templates in c++\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Templates in C++ With Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562642972\",\"position\":1,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562642972\",\"name\":\"u003cstrongu003eWhat are CPP templates?u003c\\\/strongu003e\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"In C++, a template is referred to be a design pattern or recipe for developing a generic class or function. Simply put, you may use templates to construct a single function or single class that works with several data types. A very useful element of C++ is the generic functions or classes, sometimes referred to as C++ templates. The data type variable is defined by an angled bracket in a parameter (t) and the keyword u0022templateu0022 in the C++ programming language.u003cbr\\\/u003e\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562656622\",\"position\":2,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562656622\",\"name\":\"u003cstrongu003eWhat types of templates are there in CPP?u003c\\\/strongu003eu003cbr\\\/u003e\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Function templates, class templates, and, as of C++14, variable templates are the three categories of templates. Templates are always non-variadic in older versions of C++. However, the templates in C++ can either be variadic or non-variadic.\u00a0u003cbr\\\/u003e\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562676535\",\"position\":3,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562676535\",\"name\":\"u003cstrongu003eHow many templates are there in CPP?u003c\\\/strongu003eu003cbr\\\/u003e\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"As of the latest version, CPP14, there are three main templates, namely function, class , and variable templates.u003cbr\\\/u003e\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562689465\",\"position\":4,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562689465\",\"name\":\"u003cstrongu003eWhy do we use templates in CPP?u003c\\\/strongu003eu003cbr\\\/u003e\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"When creating generic constructs like queues, lists, stacks, and vectors that can be used with any type, templates come in quite handy. In contrast to inheritance and composition, which enable the reuse of object code, C++ templates enable the reuse of source code.u003cbr\\\/u003e\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562699360\",\"position\":5,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562699360\",\"name\":\"u003cstrongu003eWhat are the different types of templates?u003c\\\/strongu003eu003cbr\\\/u003e\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Different types of templates supported by C++14 are, class templates, function templates, vardiac, and non-vardiac templates.\u00a0u003cbr\\\/u003e\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562717768\",\"position\":6,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562717768\",\"name\":\"u003cstrongu003eWhat is the use of templates?u003c\\\/strongu003eu003cbr\\\/u003e\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"In general, an instrument for imposing a consistent layout, appearance, and feel across several pages or inside content sections is a template. Any pages or regions that are based on a template are immediately modified whenever a template is altered. This concept is applied in codes as well, so that we can use a defined layout multiple times.\u00a0u003cbr\\\/u003e\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562732343\",\"position\":7,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562732343\",\"name\":\"u003cstrongu003eWhat are two types of templates?u003c\\\/strongu003eu003cbr\\\/u003e\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"The two major categories of templates are \u2013 class templates and function templates.u003cbr\\\/u003e\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562748443\",\"position\":8,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562748443\",\"name\":\"u003cstrongu003eWhat is a template example?u003c\\\/strongu003eu003cbr\\\/u003e\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"A class or a function whose layout can be used as many times as required in a code is made into a template. For example, we can create templates for functions like sort(), minimum(), maximum(), arrayPrint(), Fibonacci(), etc. Similarly, we can create class templates for heavy structures, for instance, linkedList(), Queue(), Stack(), Book(), etc.\u00a0u003cbr\\\/u003e\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562757475\",\"position\":9,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/templates-in-cpp\\\/#faq-question-1663562757475\",\"name\":\"u003cstrongu003eWhat is a template and what are its advantages?u003c\\\/strongu003eu003cbr\\\/u003e\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"With the aid of C++ templates, you can create a group of classes or functions that can handle various forms of data. When there is a need to duplicate the same code across many types, we use templates. Several advantages of templates are as follows:u003cbr\\\/u003eThey increase the efficiency of the program by reducing the developing-time when used in combination with STLu003cbr\\\/u003eThey permit type generalization.u003cbr\\\/u003eThey reduce the quantity of repetitive code you must type.u003cbr\\\/u003eThey assist in writing type-safe code.u003cbr\\\/u003eThey are assessed during compilation.u003cbr\\\/u003eThey aid in creating extremely powerful libraries\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Templates in C++","description":"A template in c++ is defined as a blueprint for creating a generic class or a function. Templates are a very powerful feature of C++.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Templates in C++ With Examples","og_description":"A template in c++ is defined as a blueprint for creating a generic class or a function. Templates are a very powerful feature of C++.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2023-11-08T04:30:20+00:00","article_modified_time":"2025-01-06T14:36:05+00:00","og_image":[{"width":1200,"height":628,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Templates in C++ With Examples","datePublished":"2023-11-08T04:30:20+00:00","dateModified":"2025-01-06T14:36:05+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/"},"wordCount":2371,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4.jpg","articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/","url":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/","name":"Templates in C++","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4.jpg","datePublished":"2023-11-08T04:30:20+00:00","dateModified":"2025-01-06T14:36:05+00:00","description":"A template in c++ is defined as a blueprint for creating a generic class or a function. Templates are a very powerful feature of C++.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562642972"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562656622"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562676535"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562689465"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562699360"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562717768"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562732343"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562748443"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562757475"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4.jpg","width":1200,"height":628,"caption":"templates in c++"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Templates in C++ With Examples"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562642972","position":1,"url":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562642972","name":"u003cstrongu003eWhat are CPP templates?u003c\/strongu003e","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"In C++, a template is referred to be a design pattern or recipe for developing a generic class or function. Simply put, you may use templates to construct a single function or single class that works with several data types. A very useful element of C++ is the generic functions or classes, sometimes referred to as C++ templates. The data type variable is defined by an angled bracket in a parameter (t) and the keyword u0022templateu0022 in the C++ programming language.u003cbr\/u003e","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562656622","position":2,"url":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562656622","name":"u003cstrongu003eWhat types of templates are there in CPP?u003c\/strongu003eu003cbr\/u003e","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Function templates, class templates, and, as of C++14, variable templates are the three categories of templates. Templates are always non-variadic in older versions of C++. However, the templates in C++ can either be variadic or non-variadic.\u00a0u003cbr\/u003e","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562676535","position":3,"url":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562676535","name":"u003cstrongu003eHow many templates are there in CPP?u003c\/strongu003eu003cbr\/u003e","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"As of the latest version, CPP14, there are three main templates, namely function, class , and variable templates.u003cbr\/u003e","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562689465","position":4,"url":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562689465","name":"u003cstrongu003eWhy do we use templates in CPP?u003c\/strongu003eu003cbr\/u003e","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"When creating generic constructs like queues, lists, stacks, and vectors that can be used with any type, templates come in quite handy. In contrast to inheritance and composition, which enable the reuse of object code, C++ templates enable the reuse of source code.u003cbr\/u003e","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562699360","position":5,"url":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562699360","name":"u003cstrongu003eWhat are the different types of templates?u003c\/strongu003eu003cbr\/u003e","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Different types of templates supported by C++14 are, class templates, function templates, vardiac, and non-vardiac templates.\u00a0u003cbr\/u003e","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562717768","position":6,"url":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562717768","name":"u003cstrongu003eWhat is the use of templates?u003c\/strongu003eu003cbr\/u003e","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"In general, an instrument for imposing a consistent layout, appearance, and feel across several pages or inside content sections is a template. Any pages or regions that are based on a template are immediately modified whenever a template is altered. This concept is applied in codes as well, so that we can use a defined layout multiple times.\u00a0u003cbr\/u003e","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562732343","position":7,"url":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562732343","name":"u003cstrongu003eWhat are two types of templates?u003c\/strongu003eu003cbr\/u003e","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"The two major categories of templates are \u2013 class templates and function templates.u003cbr\/u003e","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562748443","position":8,"url":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562748443","name":"u003cstrongu003eWhat is a template example?u003c\/strongu003eu003cbr\/u003e","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"A class or a function whose layout can be used as many times as required in a code is made into a template. For example, we can create templates for functions like sort(), minimum(), maximum(), arrayPrint(), Fibonacci(), etc. Similarly, we can create class templates for heavy structures, for instance, linkedList(), Queue(), Stack(), Book(), etc.\u00a0u003cbr\/u003e","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562757475","position":9,"url":"https:\/\/www.mygreatlearning.com\/blog\/templates-in-cpp\/#faq-question-1663562757475","name":"u003cstrongu003eWhat is a template and what are its advantages?u003c\/strongu003eu003cbr\/u003e","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"With the aid of C++ templates, you can create a group of classes or functions that can handle various forms of data. When there is a need to duplicate the same code across many types, we use templates. Several advantages of templates are as follows:u003cbr\/u003eThey increase the efficiency of the program by reducing the developing-time when used in combination with STLu003cbr\/u003eThey permit type generalization.u003cbr\/u003eThey reduce the quantity of repetitive code you must type.u003cbr\/u003eThey assist in writing type-safe code.u003cbr\/u003eThey are assessed during compilation.u003cbr\/u003eThey aid in creating extremely powerful libraries","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4.jpg",1200,628,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4-300x157.jpg",300,157,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4-768x402.jpg",768,402,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4-1024x536.jpg",1024,536,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4.jpg",1200,628,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4.jpg",1200,628,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4-640x628.jpg",640,628,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4-96x96.jpg",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/09\/Blog-Featured-Image-4-150x79.jpg",150,79,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":1,"uagb_excerpt":"Templates in c++ is defined as a blueprint or formula for creating a generic class or a function. Generic Programming is an approach to programming where generic types are used as parameters in algorithms to work for a variety of data types.In C++, a template is a straightforward yet effective tool. To avoid having to&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/25674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=25674"}],"version-history":[{"count":62,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/25674\/revisions"}],"predecessor-version":[{"id":111515,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/25674\/revisions\/111515"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/81862"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=25674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=25674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=25674"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=25674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}