{"id":6119,"date":"2021-04-22T18:45:48","date_gmt":"2021-04-22T18:45:48","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/and-the-winner-is-templates\/"},"modified":"2021-04-22T18:45:48","modified_gmt":"2021-04-22T18:45:48","slug":"and-the-winner-is-templates","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/and-the-winner-is-templates\/","title":{"rendered":"And The Winner is: Templates"},"content":{"rendered":"<p>The future of C++ speaks templates, and so does my blog Modernes C++. Based on the poll of my last post, &#8220;Quo Vadis &#8211; Modernes C++&#8221;, my next presumably 50 &#8211; 100 posts explain the details of templates. I want to give you an idea of the upcoming posts in this post.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6118\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/04\/result.png\" alt=\"result\" width=\"500\" height=\"264\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/04\/result.png 884w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/04\/result-300x158.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/04\/result-768x405.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>Let me first write a few words about the poll because its result surprised me in two ways.<\/p>\n<h2>The Poll<\/h2>\n<p>First, about 400 people voted in the poll. This is a number I would not have expected. Thanks for the many votes.<\/p>\n<p>Second, I assumed that &#8220;C++23&#8221; would probably win the poll, followed by &#8220;Templates&#8221;&nbsp; or &#8220;Extending and embedding Python with C\/C++&#8221;. My assumption was so wrong. I&#8217;m happy that I made the poll.<\/p>\n<p>When I finish these &#8220;Templates&#8221;,&nbsp; I make the next poll. Now, let&#8217;s tear the wheel to templates.<\/p>\n<\/p>\n<h2>Templates<\/h2>\n<p>First of all: What are templates? Templates (class templates or function templates) are families of classes or functions.<\/p>\n<p>Before I present my plan, I want to encourage you to write me an e-mail if I forgot a template-related feature you are interested in. Here is my e-mail address: <a href=\"mailto:Rainer.Grimm@modernescpp.de\">Rainer.Grimm@modernescpp.de<\/a>.<\/p>\n<p>These are the upcoming topics with a few additional words:<\/p>\n<h3>Basics<\/h3>\n<p>Of course, my tour starts with the basics.<\/p>\n<ul>\n<li>Presenting the instantiation of&nbsp;<strong>function templates <\/strong>provides you with the first intuition about templates. This holds in particular if function overloading or explicit template arguments are given.<strong><br \/><\/strong><\/li>\n<li>In contrast to function templates, only the C++17-compiler can deduce the template arguments for<strong> class templates<\/strong>. Meaning before C++17, you have to specify the template arguments, such as <code>int<\/code> in <code>std::vector&lt;int&gt;<\/code>.<\/li>\n<li><strong>Template parameters<\/strong> can be types, non-types, and templates. In particular, floating-point values can be used in C++20 as non-type template parameters.<\/li>\n<li>The compiler can automatically deduce the<strong> template arguments<\/strong> of templates. It makes a big difference if the template parameter is a reference\/pointer, a universal reference (&amp;&amp;), or a value. Templates can also have default template arguments.<\/li>\n<li>Class templates allow partial and full<strong> template specialization<\/strong>, and function templates only full template specialization.<\/li>\n<\/ul>\n<h3>Details<\/h3>\n<p>After the basics, I want to provide you with more details.<\/p>\n<ul>\n<li>In general,<strong> template instantiati<\/strong>on happens implicitly but can also be done explicitly.<\/li>\n<li>A <strong>variadic template<\/strong> is a template that can have an arbitrary number of parameters. Hence, templates can take arbitrary arguments of any value category.<\/li>\n<li>Based on variadic templates in C++11, we got<strong> fold expressions<\/strong> in C++17. Fold expressions reduce parameter packs on binary operators.<\/li>\n<li><strong>The friendship<\/strong> of templates is special. A class or class template can grant a general friendship to each class or function template instance. The friendship can also be granted to specific instances of a class or function template and a specific type.<\/li>\n<li>Names can be dependent on template parameters. For <strong>dependent names<\/strong>, you have to help the compiler to decide if the name is a type, a non-type, or a template. If ambiguous, the compiler assumes that a name is a non-type.<\/li>\n<\/ul>\n<h3>Techniques<\/h3>\n<p>There are many techniques and features related to templates and template instantiation.<\/p>\n<ul>\n<li>When you use a function template with at least two template parameters, you cannot decide in general, what should be the return type of the function template. Here, the <strong>automatic return typ<\/strong>e comes to our rescue.&nbsp;<\/li>\n<li><strong>Template metaprogramming<\/strong> is <a href=\"https:\/\/en.wikipedia.org\/wiki\/Turing_completeness\">turing complete<\/a>. This means all that is calculable can be calculated at compile time. Template metaprogramming is a pure functional language embedded into the imperative language C++. Libraries such as many of the <a href=\"https:\/\/www.boost.org\/\">boost <\/a>libraries use heavy template metaprogramming.<\/li>\n<li>The <strong>type traits<\/strong> library is applied to template metaprogramming. Thanks to the type traits library, you can perform type checks, type comparisons, and type modifications at compile time.<\/li>\n<li><strong><code>constexpr <\/code><\/strong>is the convenient way to program at compile time. In contrast to programming in a functional style using template metaprogramming, you can program&nbsp; <code>constexpr<\/code> in an imperative style.<code><br \/><\/code><\/li>\n<li><strong>constexpr if<\/strong> enables it to compile source code conditionally.<\/li>\n<\/ul>\n<h3>Design<\/h3>\n<p>Templates open C++ to new ways to design software.<\/p>\n<ul>\n<li>Besides <strong>dynamic polymorphism<\/strong> (object orientation), we have&nbsp;<strong>static polymorphism<\/strong> (templates).<\/li>\n<li>CRTP (curiously recurring template pattern) is a mind-blowing but potent technique. Thanks to CRTP, we can move the virtuality from run time to compile time.<\/li>\n<li><strong>Expression templates<\/strong> create structures, which stand for calculations. They are only evaluated when needed and spare time and memory.<\/li>\n<li>There are many<strong> idioms and patterns<\/strong> related to templates. For example, we have policies and traits, tag dispatching, and type erasure.<\/li>\n<\/ul>\n<h3>C++20<\/h3>\n<ul>\n<li><b>Concepts<\/b> are an extension to the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Template_(C%2B%2B)\" title=\"Template (C++)\">templates<\/a> feature provided by the <a href=\"https:\/\/en.wikipedia.org\/wiki\/C%2B%2B\" title=\"C++\">C++<\/a> programming language. Concepts are named <a href=\"https:\/\/en.wikipedia.org\/wiki\/Boolean_value\" class=\"mw-redirect\" title=\"Boolean value\">Boolean<\/a> predicates on template parameters, and evaluated at <a href=\"https:\/\/en.wikipedia.org\/wiki\/Compile_time\" title=\"Compile time\">compile time<\/a>. A concept may be associated with a template (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Class_(C%2B%2B)\" class=\"mw-redirect\" title=\"Class (C++)\">class<\/a> template, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Function_(computer_programming)\" class=\"mw-redirect\" title=\"Function (computer programming)\">function<\/a> template, or <a href=\"https:\/\/en.wikipedia.org\/wiki\/Member_function\" class=\"mw-redirect\" title=\"Member function\">member function<\/a> of a class template), in which case it serves as a <i>constraint<\/i>: it limits the set of arguments accepted as template parameters. (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Concepts_(C%2B%2B)\">Wikipedia<\/a>)<\/li>\n<\/ul>\n<h2>What&#8217;s next?<\/h2>\n<p>My next post introduces function templates using the online compiler<a href=\"https:\/\/www.google.com\/search?client=firefox-b-d&amp;q=C%2B%2B+Insights\"> C++ Insights<\/a>.<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The future of C++ speaks templates, and so does my blog Modernes C++. Based on the poll of my last post, &#8220;Quo Vadis &#8211; Modernes C++&#8221;, my next presumably 50 &#8211; 100 posts explain the details of templates. I want to give you an idea of the upcoming posts in this post.<\/p>\n","protected":false},"author":21,"featured_media":6118,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[376],"tags":[],"class_list":["post-6119","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\/6119","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=6119"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6119\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6118"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}