<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Shattered Terminal &#187; template</title>
	<atom:link href="http://shatteredterminal.com/tag/template/feed/" rel="self" type="application/rss+xml" />
	<link>http://shatteredterminal.com</link>
	<description>i don't have a tagline yet</description>
	<lastBuildDate>Sun, 11 Apr 2010 18:13:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Template concept feature (C++0x)</title>
		<link>http://shatteredterminal.com/2008/10/template-concept-feature-c0x/</link>
		<comments>http://shatteredterminal.com/2008/10/template-concept-feature-c0x/#comments</comments>
		<pubDate>Sat, 01 Nov 2008 06:30:26 +0000</pubDate>
		<dc:creator>shards</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++0x]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://shatteredterminal.com/?p=8</guid>
		<description><![CDATA[So I was kind of bored and went on to read parts of C++0x committee draft that were published recently (was it just last month?). A copy is available at the C++ standards committee website. I have heard of some new interesting stuffs that&#8217;s coming with C++0x, but wasn&#8217;t really paying attention to their specifics. [...]]]></description>
			<content:encoded><![CDATA[<p>So I was kind of bored and went on to read parts of C++0x committee draft that were published recently (was it just last month?). A <a title="C++0x Committee Draft" href="http://www.open-std.org/JTC1/SC22/WG21/docs/projects#14882">copy</a> is available at the C++ standards committee website. I have heard of some new interesting stuffs that&#8217;s coming with C++0x, but wasn&#8217;t really paying attention to their specifics. So when I read them in the standard paper, the specifics started to blow me out! The one thing that interest me a lot (other than all those functional programming&#8217;s lambdas) is template concept (§14.9.2).</p>
<p>As you may already know, C++ has long supported generic programming through template. Simple example:</p>
<pre name="code" class="cpp">template &lt;typename T, typename U&gt;
class pair {
 public:
  pair(T t, U u) : first(t), second(u) {}
  T first;
  U second;
};</pre>
<p>That defines a pair container that can contain a pair of anything! Straightforward, isn&#8217;t it? <em>It is</em>, up to a point. Note that this is also a valid template:</p>
<pre name="code" class="cpp">template &lt;typename Runnable&gt;
void schedule(Runnable* task) {
  // Let's delegate to a scheduler
  // to schedule the task.
  scheduler_-&gt;run(task);
}

class scheduler {
  &lt;..&gt;
  template &lt;typename Runnable&gt;
  void run(Runnable* task) {
    // Do something and then run the task.
    &lt;..&gt;
    task-&gt;run();
  }
}

// somewhere in main:
schedule(some_task);
</pre>
<p>What does this template requires? Yes, it needs Runnable to actually implement <code>run</code> method. Now remember that while we name the <code>typename Runnable</code>, there is nothing stopping us from using <code>typename T</code> for it. Runnable is <strong>not</strong> an interface! Let&#8217;s say some_task actually does not implement run method. However, when the compiler encountered <code>schedule(some_task);</code> in <code>main</code>, it has no way to determine what <code>some_task</code> needs to implement (it does not know that <code>some_task</code> needs to have <code>run</code> method). Similarly when <code>scheduler_-&gt;run(task);</code> is called, the compiler still has no reason to reject it. It only rejects the program when it encounters <code>task-&gt;run();</code>. At that point the compiler will throw several cryptic line that basically says, hey <code>task</code> does not implements run in <code>scheduler#run</code>, which is instantiated in <code>schedule</code> function, which is instantiated in <code>main</code>. In reality, that is 3 lines of compiler error for 1 error. If you use standard library that relies on templates heavily, it is not uncommon to have 10+ lines of compiler errors, each lines averaging at about 200 characters, just for 1 template error. I have had my share of &#8220;debugging&#8221; compiler error message.</p>
<p><strong>Template concept</strong></p>
<p>This is where <em>concept</em> comes in. Concept allows you to provide requirements for a typename. For the above example, using concept, we can transform it into:</p>
<pre name="code" class="cpp">auto concept Runnable&lt;typename T&gt; {
  void T::run();
}

template &lt;Runnable T&gt;
  void schedule(T task) {
  &lt;..&gt;
}</pre>
<p>Now <code>Runnable</code> becomes a kind of interface (well, the committee calls it concept). Now the compiler could easily tells, the moment <code>schedule</code> is called, whether <code>task</code> satisfies the requirement of needing <code>run</code> method, instead of analyzing further until it encounters the requirement implicitly in <code>scheduler#run</code>. It can then inform the developer the moment he tries to use template with conceptually incompatible type. Neat huh?</p>
<p>It&#8217;s very powerful too. It allows several different type of requirement:</p>
<ul>
<li>Availability of a non-member operator exists;</li>
<li>Availability static/non-static c-tor/d-tor/method (may specify different overloaded methods as well) exists;</li>
<li>Availability of a <code>operator new</code> and <code>delete</code> (these are necessarily member operator, hence the special case);</li>
<li>Axioms; etc.</li>
</ul>
<p>There are more interesting concept-related stuffs. Concept refinement is of course possible (similar to class inheritance; heck, even the syntax is similar). You can also specify a default if the requirement is not satisfied. The draft gave one example of this:</p>
<pre name="code" class="cpp">concept EqualityComparable&lt;typename T&gt; {
  bool operator==(T, T);
  bool operator!=(T x, T y) { return !(x == y); }
}</pre>
<p>That&#8217;s freakin&#8217; awesome! While <code>T</code> may not have <code>operator!=</code> defined, you can provide a default implementation that will work as long as <code>T</code> have <code>operator==</code> defined.</p>
<p><strong>Effect on compilation speed</strong></p>
<p>At first I was excited to think that this will probably speed up compilation of bigger codebase, only to realize a minute later that I was wrong, so completely wrong. Concept provides a set of requirements that a typename must satisfy, but the template itself may use other methods/operators not specified by the concept! That means that the usual static type-checking must still be done, in addition to checking that the typename satisfies the concept! So compilation speed definitely goes down. That&#8217;s a definite <em>blah</em>, but the benefits seem to far outweigh the additional burden placed at the compiler.</p>
]]></content:encoded>
			<wfw:commentRss>http://shatteredterminal.com/2008/10/template-concept-feature-c0x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

