<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Templates on Cheat Sheet</title><link>/kr/tags/templates/</link><description>Recent content in Templates on Cheat Sheet</description><generator>Hugo</generator><language>kr</language><atom:link href="/kr/tags/templates/index.xml" rel="self" type="application/rss+xml"/><item><title>Templates and Generic Programming</title><link>/kr/programming/cpp/primer/templates/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/kr/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;&lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; 16장 Templates and Generic Programming을 바탕으로 function / class template의 instantiation 원리, &lt;strong&gt;Template Argument Deduction&lt;/strong&gt;, &lt;strong&gt;Forwarding Reference&lt;/strong&gt; (&lt;code&gt;T&amp;amp;&amp;amp;&lt;/code&gt;) 및 &lt;strong&gt;Reference Collapsing&lt;/strong&gt;, &lt;strong&gt;Variadic Templates&lt;/strong&gt; (&lt;code&gt;Args...&lt;/code&gt;)와 &lt;code&gt;emplace_back&lt;/code&gt;의 내부 원리, 그리고 &lt;strong&gt;Template Specialization&lt;/strong&gt;을 정리한다.&lt;/p&gt;
&lt;h2 id="1-function-template과-class-template의-기본-원리"&gt;1. Function Template과 Class Template의 기본 원리&lt;a class="anchor" href="#1-function-template%ea%b3%bc-class-template%ec%9d%98-%ea%b8%b0%eb%b3%b8-%ec%9b%90%eb%a6%ac"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Template Instantiation:&lt;/strong&gt; template 자체는 실행 코드가 아니라 컴파일러를 위한 &lt;strong&gt;blueprint&lt;/strong&gt;다. 개발자가 &lt;code&gt;compare(1, 0)&lt;/code&gt;이나 &lt;code&gt;std::vector&amp;lt;std::string&amp;gt;&lt;/code&gt;처럼 template을 사용하는 순간, 컴파일러가 type parameter &lt;code&gt;T&lt;/code&gt;에 실제 타입을 대입해 구체적인 함수나 class를 생성한다.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;왜 template의 선언과 정의는 모두 헤더 파일(&lt;code&gt;.h&lt;/code&gt;)에 있어야 하는가?&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;일반 함수는 선언만 보고도 호출 코드를 컴파일한 뒤 링커가 &lt;code&gt;.o&lt;/code&gt; 목적 파일을 연결할 수 있지만, &lt;strong&gt;template은 컴파일러가 구체적인 타입으로 코드를 직접 인스턴스화해야 하므로 호출하는 소스 파일에서 template의 전체 본문 코드를 반드시 볼 수 있어야 한다.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="1-type-parametertypename--class와-non-type-parameter"&gt;1. Type Parameter(&lt;code&gt;typename&lt;/code&gt; / &lt;code&gt;class&lt;/code&gt;)와 Non-type Parameter&lt;a class="anchor" href="#1-type-parametertypename--class%ec%99%80-non-type-parameter"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Template parameter로는 type parameter(&lt;code&gt;typename T&lt;/code&gt;)뿐만 아니라 정수, 포인터, 참조 등 &lt;strong&gt;컴파일 타임 상수 값&lt;/strong&gt;을 받는 &lt;strong&gt;Non-type Template Parameter&lt;/strong&gt;도 사용할 수 있다:&lt;/p&gt;</description></item></channel></rss>