{"id":6250,"date":"2021-11-12T09:00:21","date_gmt":"2021-11-12T09:00:21","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/template-metaprogramming-hybrid-programming\/"},"modified":"2024-07-22T16:06:57","modified_gmt":"2024-07-22T16:06:57","slug":"template-metaprogramming-hybrid-programming","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/template-metaprogramming-hybrid-programming\/","title":{"rendered":"Template Metaprogramming &#8211; Hybrid Programming"},"content":{"rendered":"<p>First of all, hybrid programming is not an official term. I created it to emphasize an exciting aspect of templates. The difference between function arguments and template arguments.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6240\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/10\/TemplateMetaprogramming.png\" alt=\"TemplateMetaprogramming\" width=\"650\" height=\"403\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/10\/TemplateMetaprogramming.png 907w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/10\/TemplateMetaprogramming-300x186.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/10\/TemplateMetaprogramming-768x476.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>I ended my last post, &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/template-metaprogramming-how-it-works\">Template Metaprogramming &#8211; How it Works<\/a>&#8221; with a riddle. Here is the context for the riddle.<\/p>\n<h2>The Riddle<\/h2>\n<p>The function <code>power<\/code> and <code>Power<\/code> calculate the pow(2, 10). <code>power<\/code> is executed at run time and <code>Power<\/code> at compile time.<\/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;\">\/\/ power.cpp<\/span>\n\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;\">power<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> m, <span style=\"color: #007788; font-weight: bold;\">int<\/span> n) {                               \n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> r <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>;\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> k <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>; k <span style=\"color: #555555;\">&lt;=<\/span> n; <span style=\"color: #555555;\">++<\/span>k) r <span style=\"color: #555555;\">*=<\/span> m;\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> r;                                        \n}\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span> m, <span style=\"color: #007788; font-weight: bold;\">int<\/span> n<span style=\"color: #555555;\">&gt;<\/span>                              \n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Power {\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #006699; font-weight: bold;\">const<\/span> value <span style=\"color: #555555;\">=<\/span> m <span style=\"color: #555555;\">*<\/span> Power<span style=\"color: #555555;\">&lt;<\/span>m, n<span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;::<\/span>value;\n};\n                          \n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span> m<span style=\"color: #555555;\">&gt;<\/span>                                     \n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Power<span style=\"color: #555555;\">&lt;<\/span>m, <span style=\"color: #ff6600;\">0<\/span><span style=\"color: #555555;\">&gt;<\/span> {                                   \n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #006699; font-weight: bold;\">const<\/span> value <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>;                       \n};\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\n\t\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\t\n\t\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"power(2, 10)= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> power(<span style=\"color: #ff6600;\">2<\/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;\">\"Power&lt;2,10&gt;::value= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> Power<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">10<\/span><span style=\"color: #555555;\">&gt;::<\/span>value <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\t\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n}\n<\/pre>\n<\/div>\n<p>If you want more details about both functions, read my previous post, &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/template-metaprogramming-how-it-works\">Template Metaprogramming &#8211; How it Works<\/a>&#8220;.<\/p>\n<p>So far, so good, but what is happening in the following 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;\">\/\/ powerHybrid.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span> n<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> Power(<span style=\"color: #007788; font-weight: bold;\">int<\/span> m){\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> m <span style=\"color: #555555;\">*<\/span> Power<span style=\"color: #555555;\">&lt;<\/span>n<span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;<\/span>(m);\n}\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;&gt;<\/span>\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> Power<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">0<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> m){\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">1<\/span>;\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> <span style=\"color: #cc3300;\">\"Power&lt;0&gt;(10): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> Power<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">0<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">20<\/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;\">\"Power&lt;1&gt;(10): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> Power<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;<\/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;\">\"Power&lt;2&gt;(10): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> Power<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">2<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">10<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n    \n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n}<\/pre>\n<\/div>\n<p>As expected, <code>Power<\/code> does its job.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5611\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/01\/powerHybrid.png\" alt=\"powerHybrid\" width=\"350\" height=\"226\" \/><\/p>\n<p>Here is the riddle in short one more: Is <code>Power<\/code> a function or a metafunction?<\/p>\n<h2>Hybrid Programming<\/h2>\n<p>To make it short.<\/p>\n<p>The calls<code> Power&lt;0&gt;(10)<\/code>,<code> Power&lt;1&gt;(10)<\/code>, and <code>Power&lt;2&gt;(10)<\/code> use sharp and round brackets and calculate 10 to the power of 0, 1, and 2. This means 0, 1, and 2 are compile-time arguments, and 10 is a run-time argument. To say it differently: Power is, at the same time, a function and a metafunction. Let me elaborate more on this point.<\/p>\n<h3>Power at Run Time<\/h3>\n<p>First, I can instantiate <code>Power<\/code> for 2, give it the name<code> Power2<\/code> and use it in a for-loop.<\/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;\">\/\/ powerHybridRuntime.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span> n<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> Power(<span style=\"color: #007788; font-weight: bold;\">int<\/span> m){\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> m <span style=\"color: #555555;\">*<\/span> Power<span style=\"color: #555555;\">&lt;<\/span>n<span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;<\/span>(m);\n}\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;&gt;<\/span>\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> Power<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">0<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> m){\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">1<\/span>;\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    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> Power2of <span style=\"color: #555555;\">=<\/span> Power<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">2<\/span><span style=\"color: #555555;\">&gt;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;=<\/span> <span style=\"color: #ff6600;\">20<\/span>; <span style=\"color: #555555;\">++<\/span>i) {\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Power2of(\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> i <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\")= \"<\/span>\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> Power2of(i)  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n     }\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n}<\/pre>\n<\/div>\n<p><code>Power2o<\/code>f enables it to calculate the squares of 0 &#8230; 20 at run time.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6248\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/11\/powerHybridRuntime.png\" alt=\"powerHybridRuntime\" width=\"396\" height=\"617\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/11\/powerHybridRuntime.png 396w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/11\/powerHybridRuntime-193x300.png 193w\" sizes=\"auto, (max-width: 396px) 100vw, 396px\" \/><\/p>\n<p>You cannot invoke <code>Power<\/code> with different template arguments in the for-loop. Template instantiation requires a constant expression. To make it short: The following use of Power fails with a compile-time error that &#8220;<code><span class=\"linked-compiler-output-line\">the value of '<b>i<\/b>' is not usable in a constant expression<\/span><\/code>&#8220;.<\/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<p style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;=<\/span> <span style=\"color: #ff6600;\">20<\/span>; <span style=\"color: #555555;\">++<\/span>i) {<\/p>\n<p style=\"margin: 0; line-height: 125%;\">\u00a0\u00a0\u00a0 std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">&#8220;Power&lt;&#8220;<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> i <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">&#8220;&gt;(2)= &#8220;<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> Power<span style=\"color: #555555;\">&lt;<\/span>i<span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">2<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">&#8216;\\n&#8217;<\/span>;<\/p>\n<p style=\"margin: 0; line-height: 125%;\">}<\/p>\n<\/div>\n<p>Honestly, there is a more interesting difference between a function and a metafunction.<\/p>\n<h3>Power at Compile Time<\/h3>\n<p>When you study the previous program <code>powerHybrid.cpp<\/code> in <a href=\"https:\/\/cppinsights.io\/s\/e81885b4\">C++ Insights<\/a>, you see that each usage of Power with a different template argument creates a new type.<\/p>\n<p>This means that the invocation\u00a0<code> Power&lt;2&gt;(10)<\/code> causes the recursive template instantiation for <code>Power&lt;1&gt;(10)<\/code>, and <code>Power&lt;0&gt;(10)<\/code>. Here is the output of <a href=\"https:\/\/cppinsights.io\/s\/75ea99b9\">C++ Insights<\/a>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5157\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/02\/TemplateInstantiation.png\" alt=\"TemplateInstantiation\" width=\"450\" height=\"549\" \/><\/p>\n<p>To sum up my observation. Each template instantiation creates a new type.<\/p>\n<h3>Creating New Types<\/h3>\n<p>When you use a template such as <code>Power<\/code>, <code>std::vector<\/code>, or <code>std::array<\/code>, you can invoke it with two kinds of arguments: function arguments and template arguments. The function arguments go into the round brackets (<code>( ... )<\/code>) and the template arguments go into the sharp brackets (<code>&lt;...&gt;<\/code>). The template arguments create new types. Or, to put it the other way around. You can parameterize templates in two ways: at compile time with sharp brackets (<code>&lt;...&gt;<\/code>). and at run time with round brackets (<code>( ... )<\/code>.<\/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;\">auto<\/span> res1 <span style=\"color: #555555;\">=<\/span> Power<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">2<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">10<\/span>);                       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> res2 <span style=\"color: #555555;\">=<\/span> Power<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">2<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">11<\/span>);                       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> rest3 <span style=\"color: #555555;\">=<\/span> Power<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">10<\/span>);                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n\nstd<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> myVec1(<span style=\"color: #ff6600;\">10<\/span>);                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\nstd<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> myVec2(<span style=\"color: #ff6600;\">10<\/span>, <span style=\"color: #ff6600;\">5<\/span>);                 <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\nstd<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> myDouble(<span style=\"color: #ff6600;\">5<\/span>);                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n\nstd<span style=\"color: #555555;\">::<\/span>array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span> myArray1{ <span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>};          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\nstd<span style=\"color: #555555;\">::<\/span>array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span> myArray2{ <span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>};          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\nstd<span style=\"color: #555555;\">::<\/span>array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span> myArray3{ <span style=\"color: #ff6600;\">1.1<\/span>, <span style=\"color: #ff6600;\">2.2<\/span>, <span style=\"color: #ff6600;\">3.3<\/span>}; <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n<\/pre>\n<\/div>\n<ul>\n<li>(1) creates a new <code>Power<\/code> instance,<code> std::vector<\/code> of length 10, or a <code>std::array<\/code> with three elements<\/li>\n<li>(2) reuses the already created types in the previous lines (1)<\/li>\n<li>(3) creates a new type<\/li>\n<\/ul>\n<p>A few of my German readers already pointed it out. My metafunction Power has a significant flaw.<\/p>\n<h3>The Big Flaw<\/h3>\n<p>I get undefined behavior when I instantiate with a negative or too-big number.<\/p>\n<ol>\n<li><code>Power&lt;-1&gt;(10)<\/code> causes an infinite template instantiation because the boundary condition Power&lt;0&gt;(10) does not apply.<\/li>\n<li><code>Power&lt;200&gt;(10)<\/code> causes an <code>int<\/code> overflow.<\/li>\n<\/ol>\n<p>The first issues can be fixed by using a<code> static_assert<\/code> inside the <code>Power<\/code> template:<code> static_assert(n &gt;= 0, \"exponent must be &gt;= 0\");. <\/code>There is no simple solution for the second issue.<\/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;\">\/\/ powerHybridRuntimeOverflow.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span> n<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> Power(<span style=\"color: #007788; font-weight: bold;\">int<\/span> m){\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> m <span style=\"color: #555555;\">*<\/span> Power<span style=\"color: #555555;\">&lt;<\/span>n<span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;<\/span>(m);\n}\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;&gt;<\/span>\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> Power<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">0<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> m){\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">1<\/span>;\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    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> Power10of <span style=\"color: #555555;\">=<\/span> Power<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">10<\/span><span style=\"color: #555555;\">&gt;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;=<\/span> <span style=\"color: #ff6600;\">20<\/span>; <span style=\"color: #555555;\">++<\/span>i) {\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Power10of(\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> i <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\")= \"<\/span>\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> Power10of(i)  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n     }\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>The overflow starts with<code> Power10of(9). pow(9, 10) is <\/code>3,486,784,40<\/p>\n<p><code><br \/>\n<\/code><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6249\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/11\/powerHybridRuntimeOverflow.png\" alt=\"powerHybridRuntimeOverflow\" width=\"450\" height=\"526\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/11\/powerHybridRuntimeOverflow.png 524w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/11\/powerHybridRuntimeOverflow-256x300.png 256w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<h2>My Disclaimer<\/h2>\n<p>At the end of these three posts, &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/template-metaprogramming-a-introduction\">Template Metaprogramming &#8211; How it All Started<\/a>&#8220;,\u00a0&#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/template-metaprogramming-how-it-works\">Template Metaprogramming &#8211; How it Works<\/a>&#8221; about template metaprogramming, I have to make a disclaimer. I don&#8217;t want you to use templates to program at compile time. Most of the time, <code>constexpr<\/code> (C++11) or <code>consteval<\/code> (C++20 is the better choice.<\/p>\n<p>I explained template metaprogramming for two reasons.<\/p>\n<ol>\n<li>Template metaprogramming helps you better understand templates and the process of template instantiation.<\/li>\n<li>The<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/header\/type_traits\"> type-traits library<\/a> applies the idea and uses the conventions of template metaprogramming.<\/li>\n<\/ol>\n<h2>What&#8217;s next?<\/h2>\n<p>In my next post, I will write about the<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/header\/type_traits\"> type-traits library<\/a>.\u00a0 The type-traits library (C++11) is template metaprogramming in a beautiful guise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First of all, hybrid programming is not an official term. I created it to emphasize an exciting aspect of templates. The difference between function arguments and template arguments.<\/p>\n","protected":false},"author":21,"featured_media":6240,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[376],"tags":[435],"class_list":["post-6250","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-templates","tag-template-metaprogramming"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6250","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=6250"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6250\/revisions"}],"predecessor-version":[{"id":9767,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6250\/revisions\/9767"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6240"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}