{"id":5612,"date":"2019-01-10T09:25:26","date_gmt":"2019-01-10T09:25:26","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-programming-at-compile-time\/"},"modified":"2019-01-10T09:25:26","modified_gmt":"2019-01-10T09:25:26","slug":"c-core-guidelines-programming-at-compile-time","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-programming-at-compile-time\/","title":{"rendered":"C++ Core Guidelines: Programming at Compile Time"},"content":{"rendered":"<p>Today, I will continue my introduction to programming at compile time. The last post started with template metaprogramming. Here is where I pick up today and finish.<\/p>\n<p><!--more--><\/p>\n<p>Here is the big picture, before I dive in.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4782\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/06\/Overview.png\" alt=\"Overview\" width=\"500\" height=\"392\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;Only to remind you, the rules of the C++ Core Guidelines were my starting point:<\/p>\n<ul>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-metameta\">T.120: Use template metaprogramming only when you really need to<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-emulate\">T.121: Use template metaprogramming primarily to emulate concepts<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-tmp\">T.122: Use templates (usually template aliases) to compute types at compile time<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-fct\">T.123: Use <code class=\"highlighter-rouge no-highlight\">constexpr<\/code> functions to compute values at compile time<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-std-tmp\">T.124: Prefer to use standard-library TMP facilities<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-lib\">T.125: If you need to go beyond the standard-library TMP facilities, use an existing library<\/a><\/li>\n<\/ul>\n<p>We are just at the bottom of the triangle.<\/p>\n<h2>Template Metaprogramming<\/h2>\n<p>In the last post <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-for-template-metaprogramming\">C++ Core Guidelines: Rules for Template Metaprogramming<\/a>, I present a short template metaprogram that removed the constness from its arguments.<\/p>\n<p>&nbsp;<\/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: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> removeConst{ \r\n    <span style=\"color: #006699; font-weight: bold;\">typedef<\/span> T type;               <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\r\n};\r\n\r\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>              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> removeConst<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">const<\/span> T<span style=\"color: #555555;\">&gt;<\/span> { \r\n    <span style=\"color: #006699; font-weight: bold;\">typedef<\/span> T type;               <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\r\n};\r\n\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>is_same<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, removeConst<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;::<\/span>type<span style=\"color: #555555;\">&gt;::<\/span>value;        <span style=\"color: #0099ff; font-style: italic;\">\/\/ true (1)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>is_same<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, removeConst<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;::<\/span>type<span style=\"color: #555555;\">&gt;::<\/span>value;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ true (3)<\/span>\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>What can we learn from this example?<\/p>\n<ol>\n<li>Template specialization (partial or full) is a compile-time if.&nbsp; More specifically, when I use <span style=\"font-family: courier new, courier;\">removeConst<\/span> with a non-constant <span style=\"font-family: courier new, courier;\">int<\/span> (line 1), the compiler chooses the primary or general template (line 2).&nbsp; When I use a constant <span style=\"font-family: courier new, courier;\">int<\/span> (line 3), the compiler chooses the partial specialization for <span style=\"font-family: courier new, courier;\">const T<\/span> (line 4).&nbsp;<\/li>\n<li>The expression <span style=\"font-family: courier new, courier;\">typedef T type<\/span> serves as a return&nbsp;value which is, in this case, a type.<\/li>\n<\/ol>\n<p>Now, it has become funny.<\/p>\n<\/p>\n<h3>More Meta<\/h3>\n<p>At runtime, we use data and functions. At compile time, we use metadata and metafunctions. Relatively easy, it&#8217;s called meta because we do metaprogramming, but what is metadata or a metafunction? Here is the first definition.<\/p>\n<ul>\n<li><strong>Metadata<\/strong>: Types and integral values that are used in metafunctions.<\/li>\n<li><strong>Metafunction<\/strong>: Functions that are executed at a compile-time.<\/li>\n<\/ul>\n<p>Let me elaborate more on the terms metadata and metafunction.<\/p>\n<h4>Metadata<\/h4>\n<p>Metadata includes three entities:<\/p>\n<ol>\n<li>Types such as <span style=\"font-family: courier new, courier;\">int, <\/span>or <span style=\"font-family: courier new, courier;\">double.<\/span><\/li>\n<li>Non-types such as integrals, enumerators, pointers, or references<\/li>\n<li>Templates such as <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/stack\"><span style=\"font-family: courier new, courier;\">std::stack<\/span><\/a><\/li>\n<\/ol>\n<p>As I mentioned, I use only types and integrals in my examples of Template Metaprogramming.<\/p>\n<h4>Metafunction<\/h4>\n<p>Of course, this sounds strange. Types are used in template metaprogramming to simulate functions. Based on my definition of metafunctions, <span style=\"font-family: courier new, courier;\">constexpr<\/span> functions which can be executed at compile time, are also metafunctions, but this is my topic for a later post.<\/p>\n<p>Here are two types you already know from my last post&nbsp;<a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-for-template-metaprogramming\">C++ Core Guidelines: Rules for Template Metaprogramming<\/a>: <span style=\"font-family: courier new, courier;\">Factorial<\/span> and <span style=\"font-family: courier new, courier;\">RemoveConst.<\/span><\/p>\n<p>&nbsp;<\/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: #555555;\">&lt;&gt;<\/span> \r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Factorial<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;<\/span>{ \r\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>; \r\n};\r\n\r\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> \r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> removeConst<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">const<\/span> T<span style=\"color: #555555;\">&gt;<\/span> { \r\n    <span style=\"color: #006699; font-weight: bold;\">typedef<\/span> T type;  \r\n};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The first metafunction returns a value, and the second a type. The name <span style=\"font-family: courier new, courier;\">value<\/span> and <span style=\"font-family: courier new, courier;\">type <\/span>are just naming conventions for the return values. If a metafunction returns a <span style=\"font-family: courier new, courier;\">value,<\/span> it is called a value; if it returns a type, it is called a type. The <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/header\/type_traits\">type traits library<\/a> to which I come in my next post follows exactly this naming convention<\/p>\n<p>I think it is pretty enlightening to compare functions with metafunctions.<\/p>\n<h3>Functions versus Metafunctions<\/h3>\n<p>The following function <span style=\"font-family: courier new, courier;\">power<\/span> and the meta function <span style=\"font-family: courier new, courier;\">Power<\/span> calculate pow(2, 10) at runtime and compile time.<\/p>\n<p>&nbsp;<\/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>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\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){                                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> r <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>;\r\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;\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> r;                                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n}\r\n\r\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>                                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Power{\r\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;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n};\r\n                          \r\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>                                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2) <\/span>\r\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>{                                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)  <\/span>\r\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>;                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n\t\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\t\r\n\t\r\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> std<span style=\"color: #555555;\">::<\/span>endl;              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (A)<\/span>\r\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> std<span style=\"color: #555555;\">::<\/span>endl; <span style=\"color: #0099ff; font-style: italic;\">\/\/ (B)<\/span>\r\n\t\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Here are the first differences:<\/p>\n<ul>\n<li>Arguments: The function arguments go into the round brackets (&#8220;( &#8230; )&#8221;&nbsp; in line A)), and the metafunction arguments go into the sharp brackets (&#8220;&lt; &#8230;&gt;&#8221; in line B). This also holds for the definition of the function and the metafunction. The function uses round brackets and metafunction sharp brackets.<\/li>\n<li>Return value: The function uses a return statement (line 3), and the metafunction is the static integral constant <span style=\"font-family: courier new, courier;\">value<\/span>.<\/li>\n<\/ul>\n<p>I will continue this comparison when I come to <span style=\"font-family: courier new, courier;\">constexpr<\/span> functions. Anyway, here is the output of the program.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5609\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/01\/power.png\" alt=\"power\" width=\"300\" height=\"160\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/01\/power.png 599w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/01\/power-300x160.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>This was easy! Right? power is executed at runtime and Power at compile time, but what is happening here?<\/p>\n<p>&nbsp;<\/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>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\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>\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> power(<span style=\"color: #007788; font-weight: bold;\">int<\/span> m){\r\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);\r\n}\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> power<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> m){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> m;\r\n}\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;&gt;<\/span>\r\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){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">1<\/span>;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main(){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"power&lt;10&gt;(2): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> power<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">10<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">2<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> power2 <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: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    \r\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;\">10<\/span>; <span style=\"color: #555555;\">++<\/span>i){\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"power2(\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> i <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\")= \"<\/span> \r\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> power2(i) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;                        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    }\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The call <span style=\"font-family: courier new, courier;\">power&lt;10&gt;(2<\/span>)&nbsp; inline (1) uses sharp and round brackets and calculates 2 to the power of 10. This means, 10 is the compile time and 2 is the runtime argument. To say indifferently: <span style=\"font-family: courier new, courier;\">power<\/span> is a function and a metafunction. Now, I can instantiate the class template for 2 and give it the name <span style=\"font-family: courier new, courier;\">power2<\/span> (line 2). <a href=\"https:\/\/cppinsights.io\/\">CppInsight<\/a> shows me that the compiler is instantiating <span style=\"font-family: courier new, courier;\">power<\/span> for 2. Just the function argument is not bound.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5610\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/01\/power2.png\" alt=\"power2\" width=\"250\" height=\"98\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/01\/power2.png 444w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/01\/power2-300x118.png 300w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/p>\n<p>The function argument is a runtime argument and can be used in a for loop (line 3).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5611\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/01\/powerHybrid.png\" alt=\"powerHybrid\" width=\"400\" height=\"327\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/01\/powerHybrid.png 739w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/01\/powerHybrid-300x245.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>In the <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-programming-at-compile-time-with-type-traits-ii\">next post<\/a>, I will jump to the next level of the triangle and write about the type traits library. Template metaprogramming has been available in C++ since C++98, but the type traits since C++11. You may already assume it. The type-traits library is just a cultivated form of template metaprogramming.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I will continue my introduction to programming at compile time. The last post started with template metaprogramming. Here is where I pick up today and finish.<\/p>\n","protected":false},"author":21,"featured_media":4782,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[],"class_list":["post-5612","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5612","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=5612"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5612\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/4782"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}