<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Variadic Templates on Cheat Sheet</title><link>/tags/variadic-templates/</link><description>Recent content in Variadic Templates on Cheat Sheet</description><generator>Hugo</generator><language>en</language><atom:link href="/tags/variadic-templates/index.xml" rel="self" type="application/rss+xml"/><item><title>Templates and Generic Programming</title><link>/programming/cpp/primer/templates/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/templates/</guid><description>&lt;h1 id="templates-and-generic-programming"&gt;Templates and Generic Programming&lt;a class="anchor" href="#templates-and-generic-programming"&gt;#&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;Based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt;, Chapter 16 (Templates and Generic Programming), this post summarizes function and class template instantiation, template argument deduction, &lt;strong&gt;forwarding references (&lt;code&gt;T&amp;amp;&amp;amp;&lt;/code&gt;) and reference collapsing&lt;/strong&gt;, &lt;strong&gt;variadic templates (&lt;code&gt;Args...&lt;/code&gt;) and how &lt;code&gt;emplace_back&lt;/code&gt; works under the hood&lt;/strong&gt;, and template specialization.&lt;/p&gt;
&lt;h2 id="1-function-and-class-template-fundamentals"&gt;1. Function and Class Template Fundamentals&lt;a class="anchor" href="#1-function-and-class-template-fundamentals"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Template Instantiation:&lt;/strong&gt; A template itself is a &lt;strong&gt;blueprint&lt;/strong&gt;, not executable code. When you invoke &lt;code&gt;compare(1, 0)&lt;/code&gt; or declare &lt;code&gt;std::vector&amp;lt;std::string&amp;gt;&lt;/code&gt;, the compiler substitutes the concrete type for &lt;code&gt;T&lt;/code&gt; and instantiates a type-specific version of the function or class.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why Must Template Declarations and Definitions Live in Header Files (&lt;code&gt;.h&lt;/code&gt;)?&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;For an ordinary function, the compiler only needs the declaration to compile a call site, and the linker later links the &lt;code&gt;.o&lt;/code&gt; object file. &lt;strong&gt;To instantiate a template, however, the compiler must see the full template body at the call site&lt;/strong&gt; so it can generate code for the concrete type.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="1-type-parameters-typename--class-and-non-type-template-parameters"&gt;1. Type Parameters (&lt;code&gt;typename&lt;/code&gt; / &lt;code&gt;class&lt;/code&gt;) and Non-type Template Parameters&lt;a class="anchor" href="#1-type-parameters-typename--class-and-non-type-template-parameters"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In addition to type parameters (&lt;code&gt;typename T&lt;/code&gt;), templates can take &lt;strong&gt;non-type template parameters&lt;/strong&gt; representing compile-time constant expressions (&lt;code&gt;constexpr&lt;/code&gt; integers, pointers, or references):&lt;/p&gt;</description></item></channel></rss>