{"id":6190,"date":"2021-07-29T06:34:44","date_gmt":"2021-07-29T06:34:44","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/template-instantiation\/"},"modified":"2024-07-19T07:57:23","modified_gmt":"2024-07-19T07:57:23","slug":"template-instantiation","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/template-instantiation\/","title":{"rendered":"Template Instantiation"},"content":{"rendered":"<p>Template instantiation is creating a concrete function or a concrete class out of a function or class template. Creating template instantiation can be implicit (compiler-generated) or explicit (user-provided).<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6186\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/templatesInstantiation.png\" alt=\"templatesInstantiation\" width=\"650\" height=\"400\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/templatesInstantiation.png 904w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/templatesInstantiation-300x185.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/templatesInstantiation-768x472.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>When you need a template for a specific argument, the compiler auto-generates it. Sometimes, you want to remove template definitions from header files or avoid compute-power-consuming template instantiation. In this case, explicit instantiation is your friend.<\/p>\n<h2>Implicit Instantiation<\/h2>\n<p>Implicit instantiation should be your default choice. Implicit instantiation means the compiler automatically generates the concrete function or class for the provided template arguments. The compiler also deduces the template arguments from the function&#8217;s arguments. In C++17, the compiler can also deduce the template arguments for class templates.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ implicitTemplateInstantiation.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyClass<\/span>{\n <span style=\"color: #9999ff;\">public:<\/span>\n    MyClass(T t) { }\n    std<span style=\"color: #555555;\">::<\/span>string getType() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(T).name();\n    }\n};\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #007788; font-weight: bold;\">bool<\/span> isSmaller(T fir, T sec){\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">&lt;<\/span> sec;\n}\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main(){\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>boolalpha;\n  \n    std<span style=\"color: #555555;\">::<\/span>vector vec{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>};          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"vec.size(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> vec.size() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  \n    MyClass <span style=\"color: #cc00ff;\">myClass<\/span>(<span style=\"color: #ff6600;\">5<\/span>);                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"myClass.getType(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myClass.getType() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"isSmaller(5, 10): \"<\/span> \n              <span style=\"color: #555555;\">&lt;&lt;<\/span> isSmaller(<span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">10<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"isSmaller&lt;double&gt;(5.5f, 6.5): \"<\/span> \n              <span style=\"color: #555555;\">&lt;&lt;<\/span> isSmaller<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">5.5f<\/span>, <span style=\"color: #ff6600;\">6.5<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\n  \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  \n}\n<\/pre>\n<\/div>\n<p>Lines (1) and (2) use class template argument deduction (CTAG). <code>std::vector<\/code> or <code>MyClass<\/code> can deduce its type from their constructor arguments. Line (3) also deduces its template argument. In line (4), on the contrary, the template argument <code>double<\/code> is explicitly specified: <code>isSmaller&lt;double&gt;(5.5f, 6.5<\/code>).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6187\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/implicitTemplateInstantiation.png\" alt=\"implicitTemplateInstantiation\" width=\"300\" height=\"123\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/implicitTemplateInstantiation.png 749w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/implicitTemplateInstantiation-300x123.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>The compiler creates a concrete function or class for each implicit template instantiation.<a href=\"https:\/\/cppinsights.io\/s\/e8145723\"> C++Insights<\/a> visualizes this process.<\/p>\n<p>This automatic process is very comfortable but has a few drawbacks.<\/p>\n<ol>\n<li>When you implicitly instantiate a template, its definition is typically visible in a header file. However, you may not want to disclose the definition.<\/li>\n<li>When you need a template for specific template arguments, the compiler instantiates if unavailable in the concrete translation unit. A translation unit is the source file after processing the C preprocessor. Typically, the linker removes all redundant template instantiations and keeps one. This is a waste of time and space.<\/li>\n<\/ol>\n<p>Both issues can be solved with explicit template instantiation.<\/p>\n<h2>Explicit Instantiation<\/h2>\n<p>Explicit instantiation has two flavors: explicit instantiation definition and explicit instantiation declaration.<\/p>\n<ul>\n<li>Explicit instantiation definition syntax:<code> template &lt;template declaration&gt;<\/code><\/li>\n<li>Explicit instantiation declaration syntax:<code> extern template &lt;template declaration&gt;<\/code><\/li>\n<\/ul>\n<p>When you study the syntax, the keyword <code>extern<\/code> makes the difference.<\/p>\n<p>Explicit template instantiation means that you generate the definition of a template. Here is a simple example.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ explicitTemplateInstantiation.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyClass<\/span>{\n <span style=\"color: #9999ff;\">public:<\/span>\n    MyClass(T t) { }\n    std<span style=\"color: #555555;\">::<\/span>string getType() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(T).name();\n    }\n};\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #007788; font-weight: bold;\">bool<\/span> isSmaller(T fir, T sec){\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">&lt;<\/span> sec;\n}\n \n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">std<\/span><span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>;                       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #007788; font-weight: bold;\">bool<\/span> std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;::<\/span>empty() <span style=\"color: #006699; font-weight: bold;\">const<\/span>;      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyClass<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>;                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> std<span style=\"color: #555555;\">::<\/span>string MyClass<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;::<\/span>getType() <span style=\"color: #006699; font-weight: bold;\">const<\/span>; <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #cc00ff;\">isSmaller<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #007788; font-weight: bold;\">int<\/span>);                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #007788; font-weight: bold;\">bool<\/span> isSmaller<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #007788; font-weight: bold;\">double<\/span>, <span style=\"color: #007788; font-weight: bold;\">double<\/span>);       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (6)<\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\n\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  \n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>boolalpha;\n  \n  std<span style=\"color: #555555;\">::<\/span>vector vec{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>};\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"vec.size(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> vec.size() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  \n  MyClass myClass(<span style=\"color: #ff6600;\">5<\/span>);\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"myClass.getType(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myClass.getType() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  \n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  \n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"isSmaller(5, 10): \"<\/span> \n            <span style=\"color: #555555;\">&lt;&lt;<\/span> isSmaller(<span style=\"color: #ff6600;\">5<\/span>,<span style=\"color: #ff6600;\">10<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"isSmaller&lt;double&gt;(5.5f, 6.5): \"<\/span> \n            <span style=\"color: #555555;\">&lt;&lt;<\/span> isSmaller<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">5.5f<\/span>, <span style=\"color: #ff6600;\">6.5<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  \n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  \n}\n<\/pre>\n<\/div>\n<p>Lines (1) to (6) are the interesting ones. Thanks to the keyword <code>template<\/code>, explicit <code>template<\/code> instantiation happens.<\/p>\n<ul>\n<li>\n<div>Line (1) explicitly instantiated <code>std::vector<\/code> for <code>int<\/code> and line (2) its member function <code>empty<\/code> for <code>double.<\/code><\/div>\n<\/li>\n<li>\n<div>Line (3) explicitly instantiates <code>MyClass<\/code> for <code>int<\/code> and line (4) its member function <code>getType<\/code> for <code>double<\/code>.<\/div>\n<\/li>\n<li>\n<div>Line (5) explicitly instantiated <code>isSmaller<\/code> for <code>(int, int)<\/code>, and line (6) does the same for <code>(double, double) <\/code>providing the explicit template argument <code>double<\/code>.<\/div>\n<\/li>\n<\/ul>\n<h3>Hide the Template Implementation<\/h3>\n<p>How can explicit template instantiation help you hide the definition of the templates?<\/p>\n<ul>\n<li>Put the template declaration in the header file.<\/li>\n<li>Put the template definition in the source file. Explicitly instantiate the template at the end of the source file.<\/li>\n<li>Use the template by including the header file.<\/li>\n<\/ul>\n<p>Here are three files exemplifying this process.<\/p>\n<ul>\n<li>Template declaration<\/li>\n<\/ul>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ MyClass.h<\/span>\n\n<span style=\"color: #009999;\">#include &lt;typeinfo&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyClass<\/span>{\n <span style=\"color: #9999ff;\">public:<\/span>\n    MyClass(T t) { }\n    std<span style=\"color: #555555;\">::<\/span>string getType() <span style=\"color: #006699; font-weight: bold;\">const<\/span>;\n};\n<\/pre>\n<\/div>\n<ul>\n<li>Template definition and explicit instantiation for <code>int<\/code><\/li>\n<\/ul>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ MyClass.cpp<\/span>\n\n<span style=\"color: #009999;\">#include \"MyClass.h\"<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\nstd<span style=\"color: #555555;\">::<\/span>string MyClass<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>getType() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(T).name();\n}\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyClass<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>; \n<\/pre>\n<\/div>\n<ul>\n<li>Template use<\/li>\n<\/ul>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ mainMyClass.cpp<\/span>\n\n<span style=\"color: #009999;\">#include \"MyClass.h\"<\/span>\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>; \n\n    MyClass myClass(<span style=\"color: #ff6600;\">5<\/span>);\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"myClass.getType(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myClass.getType() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    <span style=\"color: #0099ff; font-style: italic;\">\/*<\/span>\n<span style=\"color: #0099ff; font-style: italic;\">    MyClass myClass2(5.5);<\/span>\n<span style=\"color: #0099ff; font-style: italic;\">    std::cout &lt;&lt; \"myClass2.getType(): \" &lt;&lt; myClass2.getType() &lt;&lt; '\\n';<\/span>\n<span style=\"color: #0099ff; font-style: italic;\">    *\/<\/span>\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n}\n<\/pre>\n<\/div>\n<p>Compiling and running the program gives the expected result.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6188\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/mainMyClass.png\" alt=\"mainMyClass\" width=\"500\" height=\"174\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/mainMyClass.png 680w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/mainMyClass-300x105.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>But when I try to use <code>MyClass<\/code> another type then <code>int<\/code>, I get a linker error. I get this linker error message when I use the commented-out lines.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6189\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/mainMyClassError.png\" alt=\"mainMyClassError\" width=\"650\" height=\"149\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/mainMyClassError.png 992w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/mainMyClassError-300x69.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/mainMyClassError-768x177.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/> There is no template instantiation\u00a0 <code>double<\/code> available.<\/p>\n<h3>Suppress the Template Instantiation<\/h3>\n<p>Assume you use <code>MyClass&lt;int<\/code>&gt; in various translation units that the linker puts together. Essentially, the linker throws away all template instantiations but one. This is a waste of computing time. Thanks to the use of the extern keyword in C++11, you can make out of an explicit template instantiation definition an explicit template instantiation declaration. What?<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyClass<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>;        <span style=\"color: #0099ff; font-style: italic;\">\/\/ explicit instantiation definition<\/span>\n<span style=\"color: #006699; font-weight: bold;\">extern<\/span> <span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyClass<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>; <span style=\"color: #0099ff; font-style: italic;\">\/\/ explicit instantiation declaration <\/span>\n<\/pre>\n<\/div>\n<p>The key observation is that the second line does not cause a template instantiation. This means that the compiler generates not something the linker throws away. You have only to ensure that one instantiation of <code>MyClass&lt;int&gt;<\/code> is for the linker available. If not, you will get a linker error.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>After this more technical post, I will write in my <a href=\"https:\/\/www.modernescpp.com\/index.php\/variadic-templates-or-the-power-of-three-dots\">next post<\/a> about variadic templates &#8230; .<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Template instantiation is creating a concrete function or a concrete class out of a function or class template. Creating template instantiation can be implicit (compiler-generated) or explicit (user-provided).<\/p>\n","protected":false},"author":21,"featured_media":6186,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[376],"tags":[],"class_list":["post-6190","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-templates"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6190"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6190\/revisions"}],"predecessor-version":[{"id":9751,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6190\/revisions\/9751"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6186"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}