{"id":6196,"date":"2021-08-06T08:06:56","date_gmt":"2021-08-06T08:06:56","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/variadic-templates-or-the-power-of-three-dots\/"},"modified":"2024-07-22T10:38:23","modified_gmt":"2024-07-22T10:38:23","slug":"variadic-templates-or-the-power-of-three-dots","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/variadic-templates-or-the-power-of-three-dots\/","title":{"rendered":"Variadic Templates or the Power of Three Dots"},"content":{"rendered":"<p>A variadic template is a template that can have an arbitrary number of template parameters. If you see it for the first time, this feature may seem magical to you. So, let me demystify variadic templates.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6191\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/templates.png\" alt=\"templates\" width=\"650\" height=\"399\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/templates.png 926w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/templates-300x184.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/templates-768x472.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>You may wonder if my graphic showing the topics I write about includes template instantiation. The reason is simple. After my last post about &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/template-instantiation\">Template Instantiation<\/a>&#8220;, one of my German readers (pseudonym <span class=\"first\">Urfahraner Auge) <\/span>commented. There is an essential difference between implicit and explicit instantiation of a template that I forgot to mention. He is right. The implicit instantiation of templates is lazy, but the explicit instantiation of templates is eager.<\/p>\n<h2>Lazy versus Eager Template Instantiation<\/h2>\n<p>Template instantiation is lazy. <code><\/code>It will not be instantiated if you don&#8217;t need a member function of a class template. Only the member function declaration is available, but its definition is not. This works so far that you can use an invalid code in a member function. Of course, the member function must not be called.<\/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;\">\/\/ numberImplicitExplicit.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;cmath&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;\">struct<\/span> Number {\n\t<span style=\"color: #007788; font-weight: bold;\">int<\/span> absValue() {\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>abs(val);\n    }\n  T val{};\n};\n\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ template class Number&lt;std::string&gt;;           \/\/ (2)<\/span>\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ template int Number&lt;std::string&gt;::absValue(); \/\/ (3)<\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\n  \n    Number<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> numb;\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ numb.absValue();                         \/\/ (1)<\/span>\n  \n}\n<\/pre>\n<\/div>\n<p>If you call the member function <code>numb.absValue()<\/code> (line 1), you get what you may expect. A compile-time error message essentially says that the is no overload<code> std::abs<\/code> for<code> std::string<\/code> available. Here are the first two lines from the verbose error message:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6192\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/numberImplicitExplicit.png\" alt=\"numberImplicitExplicit\" width=\"650\" height=\"79\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/numberImplicitExplicit.png 992w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/numberImplicitExplicit-300x37.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/numberImplicitExplicit-768x94.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>I have to explain template instantiation more precisely:<strong> The implicit instantiation of templates is lazy, but the explicit instantiation of templates is eager. <\/strong><\/p>\n<p>When you enable line (2) (<code>template\u00a0class\u00a0Number&lt;std::string&gt;<\/code>) and explicitly instantiated the class template <code>Number<\/code> or you enable line (3) (<code>template\u00a0int\u00a0Number&lt;std::string&gt;::absValue(<\/code>)) and explicitly instantiated the member function <code>absValue<\/code> for <code>std::string<\/code>, you get a compile-time error. This compile-time error is equivalent to the compiler error invoking the member function absValue in line (1) (<code>numb.absValue()<\/code>). Once more, here are the first two lines of the error messages after enabling line (2) or line (3).<\/p>\n<ul>\n<li>Line (2) enabled<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6193\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/numberImplicitExplicit2.png\" alt=\"numberImplicitExplicit2\" width=\"650\" height=\"81\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/numberImplicitExplicit2.png 944w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/numberImplicitExplicit2-300x38.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/numberImplicitExplicit2-768x96.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<ul>\n<li>Line (3) enabled<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6194\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/numberImplicitExplicit3.png\" alt=\"numberImplicitExplicit3\" width=\"650\" height=\"86\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/numberImplicitExplicit3.png 950w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/numberImplicitExplicit3-300x40.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/numberImplicitExplicit3-768x102.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<h3>A Personal Note:<\/h3>\n<p>I&#8217;m keen to get comments about my posts. They help me to write about the content I want to hear. In particular, the German community is very engaged.<\/p>\n<p>Now, finally, to something completely different: variadic templates.<\/p>\n<h2>Variadic Templates<\/h2>\n<p>A variadic template is a template that can have an arbitrary number of template parameters. If you see it for the first time, this feature may seem magical.<\/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: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> ... Args<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> variadicTemplate(Args ... args) { \n    . . . . <span style=\"color: #0099ff; font-style: italic;\">\/\/ four dots<\/span>\n}\n<\/pre>\n<\/div>\n<p>The ellipsis (<code>...<\/code>) makes <code>Args<\/code> or <code>args<\/code> a so-called parameter pack. Precisely, <code>Args<\/code> it is a template parameter pack and <code>args <\/code>a function parameter pack. Two operations are possible with parameter packs. They can be packed and unpacked. If the ellipse is to the left of <code>Args<\/code>, the parameter pack will be packed; if it is to the right of <code>Args<\/code>, it will be unpacked. Because of the function template argument deduction, the compiler can derive the template arguments.<\/p>\n<p>Variadic templates are often used in the Standard Template Library and the core language.<\/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: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span>... Types<span style=\"color: #555555;\">&gt;<\/span>                                              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">tuple<\/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> Callable, <span style=\"color: #006699; font-weight: bold;\">typename<\/span>... Args <span style=\"color: #555555;\">&gt;<\/span>                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n<span style=\"color: #006699; font-weight: bold;\">explicit<\/span> <span style=\"color: #006699; font-weight: bold;\">thread<\/span>(Callable<span style=\"color: #555555;\">&amp;&amp;<\/span> f, Args<span style=\"color: #555555;\">&amp;&amp;<\/span>... args);\t\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> Lockable1, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Lockable2, <span style=\"color: #006699; font-weight: bold;\">typename<\/span>... LockableN<span style=\"color: #555555;\">&gt;<\/span>  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> lock(Lockable1<span style=\"color: #555555;\">&amp;<\/span> lock1, Lockable2<span style=\"color: #555555;\">&amp;<\/span> lock2, LockableN<span style=\"color: #555555;\">&amp;<\/span>... lockn);\n\n<span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>...(ParameterPack);                                                 <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\n<\/pre>\n<\/div>\n<p>All four examples from the C++11 standard use variadic templates. The first three are part of the Standard Template Library. Let&#8217;s see what I can deduce from the declarations.<\/p>\n<ol>\n<li><code>std::tuple<\/code> accepts an arbitrary number of different types.<\/li>\n<li><code>std::thread<\/code> allows it to invoke a callable with an arbitrary number of arguments. The argument can have different types. You can invoke a callable, such as a function, function object, or lambda expression. The function <code>std::thread<\/code> takes its callable and its arguments by universal reference. If you need more detail: I already wrote about template argument deduction and universal references in my post &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/template-arguments\">Template Arguments<\/a>&#8220;.<\/li>\n<li><code>std::lock<\/code> It can lock an arbitrary number of lockable types in an atomic step. Locking one lockable type in an atomic step is trivial. Consequently, <code>std::lock<\/code> requires at least two arguments. <code>Lockable<\/code> is named requirement. Types supporting <code>Lockable<\/code> must have the member functions<code> lock<\/code>, <code>unlock<\/code>, and<code> try_lock<\/code>.<\/li>\n<li>The<code> sizeof ...<\/code> &#8211; operator returns the number of elements in the <code>ParameterPack<\/code>.<a title=\"cpp\/language\/parameter pack\" href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/parameter_pack\"><br \/>\n<\/a><\/li>\n<\/ol>\n<p>The<code> sizeof...<\/code>-operator seems special because the ParameterPack is used in the core language. Let me write a few words about it.<\/p>\n<h3><code>sizeof..<\/code>.-Operator<\/h3>\n<p>Thanks to the <code>sizeof<\/code> &#8230;-operator can be used to determine how many elements a parameter pack contains directly. The elements are not evaluated.<\/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;\">\/\/ printSize.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> std<span style=\"color: #555555;\">::<\/span>literals;\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> ... Args<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> printSize(Args<span style=\"color: #555555;\">&amp;&amp;<\/span> ... args){\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>...(Args) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">' '<\/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: #006699; font-weight: bold;\">sizeof<\/span>...(args) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/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    printSize();                                       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n    printSize(<span style=\"color: #cc3300;\">\"C string\"<\/span>, <span style=\"color: #cc3300;\">\"C++ string\"<\/span>s, <span style=\"color: #ff6600;\">2011<\/span>, <span style=\"color: #336666;\">true<\/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>The <code>sizeof<\/code>..-operator allows it to determine the size of the template parameter pack (1) and the function parameter pack (2) at compile time. I apply it to an empty parameter pack (3) and a parameter pack containing four elements. The first element is a C-string and the second a C++ string. To use the C++-string literal, I have to include the namespace<code> std::literals<\/code> (5). C++14 supports C++ string literals.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6195\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/printSize.png\" alt=\"printSize\" width=\"345\" height=\"243\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/printSize.png 345w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/08\/printSize-300x211.png 300w\" sizes=\"auto, (max-width: 345px) 100vw, 345px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>In my <a href=\"https:\/\/www.modernescpp.com\/index.php\/more-arbout-variadic-templates\">next post<\/a>, I will explore variadic templates more deeply and introduce the functional pattern for evaluating them. Additionally, I will present the perfect factory function and jump from C++11 to C++17: fold expression in C++17.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A variadic template is a template that can have an arbitrary number of template parameters. If you see it for the first time, this feature may seem magical to you. So, let me demystify variadic templates.<\/p>\n","protected":false},"author":21,"featured_media":6191,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[376],"tags":[441],"class_list":["post-6196","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-templates","tag-variadic-templates"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6196","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=6196"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6196\/revisions"}],"predecessor-version":[{"id":9753,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6196\/revisions\/9753"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6191"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}