<?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>Nicolás Brailovsky &#187; C++</title>
	<atom:link href="http://nicolasb.com.ar/category/programming/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://nicolasb.com.ar</link>
	<description>A modern blog</description>
	<lastBuildDate>Tue, 07 Sep 2010 10:07:33 +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>C++ linking WTF</title>
		<link>http://nicolasb.com.ar/2010/09/c-linking-wtf/</link>
		<comments>http://nicolasb.com.ar/2010/09/c-linking-wtf/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 10:07:33 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[WTF]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=972</guid>
		<description><![CDATA[It is a commonly accepted fact that a succesfuly compiled application serves as enough proof of its correctness, but common wisdom doesn&#8217;t say a thing about linking. If you like linker WTF moments, you&#8217;ll love this snippet. Can you guess why won&#8217;t it compile?



struct Foo &#123;


&#160; &#160; static const int x = 0;


&#160; &#160; static [...]]]></description>
			<content:encoded><![CDATA[<p>It is a commonly accepted fact that a succesfuly compiled application serves as enough proof of its correctness, but common wisdom doesn&#8217;t say a thing about linking. If you like linker WTF moments, you&#8217;ll love this snippet. Can you guess why won&#8217;t it compile?</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">struct</span> Foo <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> x = <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> y = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw4">int</span> z<span class="br0">&#40;</span>bool x<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#40;</span>x<span class="br0">&#41;</span>? Foo::<span class="me2">x</span> : Foo::<span class="me2">y</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="co2">#include </span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Foo z;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; std::<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; z.<span class="me1">z</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Well, it does compile (gotcha!) but it just won&#8217;t link. Yet it seems so simple&#8230; let&#8217;s add some more mistery to this WTF moment, try this change:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"> &nbsp; &nbsp;<span class="kw4">int</span> z<span class="br0">&#40;</span>bool x<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> t = Foo::<span class="me2">x</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#40;</span>x<span class="br0">&#41;</span>? t : Foo::<span class="me2">y</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>Holy shit, now it compiles? WTF? Some more strangeness:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"> &nbsp; &nbsp;<span class="kw4">int</span> z<span class="br0">&#40;</span>bool<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span>? Foo::<span class="me2">x</span> : Foo::<span class="me2">y</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>And again, now it compiles. WTF? I&#8217;ll make a final change, this one should give you a clue about why it won&#8217;t compile. Revert all changes back to the original code but add this two lines after Foo:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">const</span> <span class="kw4">int</span> Foo::<span class="me2">x</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">const</span> <span class="kw4">int</span> Foo::<span class="me2">y</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Though weird at first, now you should have a clear picture:</p>
<ul>
<li>The first case doesn&#8217;t compiles: x and y are declared in struct Foo, yet the linker doesn&#8217;t know in which translation unit they should be allocated.</li>
<li>The second and third cases&#8230; well I&#8217;m not sure why does this compiles but it&#8217;s probably because the linker can asume in which translation unit x and y should be allocated. I&#8217;m to lazy to check.</li>
<li>In the last case we explicitly say where should x and y be. According to standard, this is how these two ints should be declared.</li>
</ul>
<p>So, some linker strangeness. Beware, it&#8217;s easy to get trapped by this one.</p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2010/09/c-linking-wtf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Template Metaprogramming XIII: Heart of Darkness</title>
		<link>http://nicolasb.com.ar/2010/08/template-metaprogramming-xiii-heart-of-darkness/</link>
		<comments>http://nicolasb.com.ar/2010/08/template-metaprogramming-xiii-heart-of-darkness/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 10:00:57 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[Templates]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=1222</guid>
		<description><![CDATA[Last time we had a virtual template dispatch problem&#8230; we got to the point of knowing which was the index of the cache we were searching for, now we need to actually retrieve an instance of that cache. That&#8217;s a problem. Why? To begin with, there are no instances, only types!
The next logical step would [...]]]></description>
			<content:encoded><![CDATA[<p>Last time we had a virtual template dispatch problem&#8230; we got to the point of knowing which was the index of the cache we were searching for, now we need to actually retrieve an instance of that cache. That&#8217;s a problem. Why? To begin with, there are no instances, only types!</p>
<p>The next logical step would be to create a Map device, to map a list of types to a list of instances&#8230; let&#8217;s see how can we do that, in pseudocode</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">instances( H|T ) &lt;- [ create_instance(H), instances(T) ]
</div>
</li>
<li class="li1">
<div class="de1">instances( NIL ) &lt;- NIL</div>
</li>
</ol>
</div>
<p>Looks easy. How can we map that to c++?</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">template &lt;class Lst&gt; <span class="kw4">struct</span> Instance <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">typedef</span> typename Lst::<span class="me2">head</span> Elm;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Elm instance;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Instance&lt; typename Lst::<span class="me2">tail</span> &gt; next;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">template &lt;&gt; <span class="kw4">struct</span> Instance&lt;NIL&gt; <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;iostream&gt;</span></div>
</li>
<li class="li1">
<div class="de1">using std::<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">typedef</span> LST&lt;int, LST&lt;char, LST&lt;float&gt; &gt; &gt; Lst;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Instance&lt;Lst&gt; lst;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; lst.<span class="me1">instance</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; lst.<span class="me1">next</span>.<span class="me1">instance</span> = <span class="st0">&#8216;a&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; lst.<span class="me1">next</span>.<span class="me1">next</span>.<span class="me1">instance</span> = <span class="nu0">3.1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; std::<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; lst.<span class="me1">next</span>.<span class="me1">instance</span> &lt;&lt; <span class="st0">&quot;<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>All those next.next.next.instance look ugly. Let&#8217;s use some more meta-magic to get the Nth instance (why not a [] operator? several reasons, you can&#8217;t mix non-const ints with templates nicely, there would be problems to define the return type&#8230; all those options are workable but it&#8217;s easier if we do this in another device.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">template &nbsp;struct Nth {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; typedef typename LST::tail Tail;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; typedef typename Nth::result result;
</div>
</li>
<li class="li1">
<div class="de1">};
</div>
</li>
<li class="li2">
<div class="de2">template &nbsp;struct Nth {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; typedef typename LST::head result;
</div>
</li>
<li class="li1">
<div class="de1">};</div>
</li>
</ol>
</div>
<p>Remember that one from the toolbox? Now we know how to get a specific index position, yet getting the instance is a different problem (the Nth device returns a type, not an instance). We should do something different, the problem is knowing the return type. What&#8217;s the return type for the Nth instance of the Instances list?</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;type &lt;- Nth(TypesLst, Type)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;type var &lt;- NthInstance(InstancesLst, N)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Not so easy, right? This is the translated C++:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">template &lt;class TypeLst, <span class="kw4">int</span> N&gt; <span class="kw4">struct</span> NthInstance <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// This one isnt easy&#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// This is the next type in the list</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">typedef</span> typename TypeLst::<span class="me2">tail</span> TypeNext;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// &nbsp;* Nth::result is the Nth type in Lst (i.e. char, int, &#8230;)</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">typedef</span> typename NthInstance&lt; TypeNext, N<span class="nu0">-1</span> &gt;::<span class="me2">NthInstanceType</span> NthInstanceType;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// &nbsp;* typename Nth::result &amp;amp; is a reference to said type and the ret type</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; template &lt;class InstancesLst&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">static</span> NthInstanceType&amp; get<span class="br0">&#40;</span>InstancesLst &amp;instances_lst<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> NthInstance&lt; TypeNext, N<span class="nu0">-1</span> &gt;::<span class="me2">get</span><span class="br0">&#40;</span>instances_lst.<span class="me1">next</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Remember we chose a 1-based system (wtf..)</span></div>
</li>
<li class="li1">
<div class="de1">template &lt;class TypeLst&gt; <span class="kw4">struct</span> NthInstance&lt;TypeLst, <span class="nu0">1</span>&gt; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">typedef</span> typename TypeLst::<span class="me2">head</span> NthInstanceType;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; template &lt;class InstancesLst&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">static</span> NthInstanceType&amp; get<span class="br0">&#40;</span>InstancesLst &amp;instances_lst<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> instances_lst.<span class="me1">instance</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>And the code from fetching the instance itself is even more difficult, so I&#8217;ll leave that for next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2010/08/template-metaprogramming-xiii-heart-of-darkness/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Template Metaprogramming XII: You Really Got a Hold on Me</title>
		<link>http://nicolasb.com.ar/2010/08/template-metaprogramming-xii-you-really-got-a-hold-on-me/</link>
		<comments>http://nicolasb.com.ar/2010/08/template-metaprogramming-xii-you-really-got-a-hold-on-me/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 10:00:42 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[Templates]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=1219</guid>
		<description><![CDATA[Remember our virtual template method problem, from the other time? (I know, I said the answer was scheduled for a week after that post, but then I just forgot about it). May be we could avoid the virtual part by keeping a list of all our caches&#8230; how would we know which one should we [...]]]></description>
			<content:encoded><![CDATA[<p>Remember our virtual template method problem, from the other time? (I know, I said the answer was scheduled for a week after that post, but then I just forgot about it). May be we could avoid the virtual part by keeping a list of all our caches&#8230; how would we know which one should we dispatch the message to? Easy, using templates.</p>
<p>Instead of a list let&#8217;s keep two, for twice the fun. One for the rows cache, another for the PKs. We can use PK to know which ROW Cache should we choose. Let&#8217;s try to write a pseudo code for it:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">ROW get_row<span class="br0">&#40;</span>PK id<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; pos &lt;- Position of PK in pks_lst</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> cache<span class="br0">&#91;</span> pos <span class="br0">&#93;</span>.<span class="me1">get_row</span><span class="br0">&#40;</span> id <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>Doesn&#8217;t look too hard. Building on our previous toolbox, let&#8217;s use Eq, Position and the definition of a list:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">struct</span> NIL <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">typedef</span> NIL head;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">typedef</span> NIL tail;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">template &lt; class H, class T=NIL&gt; <span class="kw4">struct</span> LST <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">typedef</span> H head;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">typedef</span> T tail;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">template &lt;class X, class Y&gt; <span class="kw4">struct</span> Eq <span class="br0">&#123;</span> <span class="kw4">static</span> <span class="kw4">const</span> bool result = <span class="kw2">false</span>; <span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">template &lt;class X&gt; <span class="kw4">struct</span> Eq&lt;X, X&gt; <span class="br0">&#123;</span> <span class="kw4">static</span> <span class="kw4">const</span> bool result = <span class="kw2">true</span>; <span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">template &lt;class Elm, class LST&gt; <span class="kw4">struct</span> Position <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; private:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">typedef</span> typename LST::<span class="me2">head</span> H;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">typedef</span> typename LST::<span class="me2">tail</span> T;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> bool found = Eq&lt;H, Elm&gt;::<span class="me2">result</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public:</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> result = found? <span class="nu0">1</span> : <span class="nu0">1</span> + Position&lt;Elm, T&gt;::<span class="me2">result</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">template &lt;class Elm&gt; <span class="kw4">struct</span> Position&lt;Elm, NIL&gt; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> result = <span class="nu0">0</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">class Facade <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">typedef</span> LST&lt;int, LST&lt;char, LST&lt;float&gt; &gt; &gt; Lst;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; public:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; template &lt;class PK&gt; <span class="kw4">int</span> find<span class="br0">&#40;</span>PK<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> Position&lt; PK, Lst &gt;::<span class="me2">result</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;iostream&gt;</span></div>
</li>
<li class="li1">
<div class="de1">using std::<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; Facade f;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; std::<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; f.<span class="me1">find</span><span class="br0">&#40;</span><span class="nu0">1.0</span><span class="br0">&#41;</span> &lt;&lt; <span class="st0">&quot;<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Great, now we can find an element on a list of types. The real virtual dispatch for the next entry <img src='http://nicolasb.com.ar/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2010/08/template-metaprogramming-xii-you-really-got-a-hold-on-me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Namespaces and g++</title>
		<link>http://nicolasb.com.ar/2010/08/c-namespaces-and-g/</link>
		<comments>http://nicolasb.com.ar/2010/08/c-namespaces-and-g/#comments</comments>
		<pubDate>Tue, 03 Aug 2010 12:48:32 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Grumpy]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=699</guid>
		<description><![CDATA[


Have you ever tried to leave open a C++ namespace after EOF (that is, openning a namespace in a headerfile but forgetting to close it). It&#8217;s a little bit like getting your balls caught by the door. The compiler will throw at you an incredible number of seemingly unrelated errors, all of which occur in [...]]]></description>
			<content:encoded><![CDATA[<table>
<tbody>
<tr>
<td>Have you ever tried to leave open a C++ namespace after EOF (that is, openning a namespace in a headerfile but forgetting to close it). It&#8217;s a little bit like getting your balls caught by the door. The compiler will throw at you an incredible number of seemingly unrelated errors, all of which occur in a different file than the offending header.</p>
<p>Reaching EOF on a C++ file without closing all its namespaces should be ilegal; or at least you should have better error reporting, because right now it&#8217;s almost impossible to know what&#8217;s the source of the error (for g++, that is).</td>
<td><a href="http://nicolasb.com.ar/archivos/2009/10/grumpy-old-man.jpg"><img class="aligncenter size-medium wp-image-605" title="grumpy old man" src="http://nicolasb.com.ar/archivos/2009/10/grumpy-old-man-300x300.jpg" alt="" width="300" height="300" /></a></td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2010/08/c-namespaces-and-g/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Design Patterns: C++ Idiom RAII</title>
		<link>http://nicolasb.com.ar/2010/07/design-patterns-c-idiom-raii/</link>
		<comments>http://nicolasb.com.ar/2010/07/design-patterns-c-idiom-raii/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 10:00:37 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=427</guid>
		<description><![CDATA[Ohh design patterns. I love buzzwords. Who doesn&#8217;t? To increase my buzzword count I will be writing about a topic most people programming C++ should already know: RAII, resource acquisition is initialization.
Patterns everywhere




Knowing that Gamma et al didn&#8217;t list all the known patterns in the universe does come as a surprise to some, not sure [...]]]></description>
			<content:encoded><![CDATA[<p>Ohh design patterns. I love buzzwords. Who doesn&#8217;t? To increase my buzzword count I will be writing about a topic most people programming C++ should already know: RAII, resource acquisition is initialization.</p>
<h3>Patterns everywhere</h3>
<table>
<tbody>
<tr>
<td><a href="http://nicolasb.com.ar/archivos/2010/02/buzzword_box.jpg"><img class="aligncenter size-full wp-image-829" title="buzzword_box" src="http://nicolasb.com.ar/archivos/2010/02/buzzword_box.jpg" alt="" width="225" height="225" /></a></td>
<td>Knowing that Gamma et al didn&#8217;t list all the known patterns in the universe does come as a surprise to some, not sure why though. The twenty some patterns they write about in their now famous book are (arguably, perhaps) some of the most common design patterns, but the list hardly finishes there.</td>
</tr>
</tbody>
</table>
<p>Some patterns only have meaning in a very specific context; a reactor is a nice pattern for handling asynchronous events yet most applications would never need it. Sometimes &#8220;the context&#8221; means a specific domain in which the application must work, like a web application, a real time application or a distributed application, sometimes the context is the language itself. RAII falls in this last domain, it only makes sense for C++ applications (actually there are others, but thinking what kind of languages would support this idiom is left as an exercise for the reader).</p>
<h3>No, really. What is RAII?</h3>
<p>If you made it past that long intro you are probably really interested in knowing what RAII is, and don&#8217;t know how to search it in Wikipedia. OK, I&#8217;ll explain it the best I can then: RAII means that a resource acquisition is the initialization.</p>
<p>Seriously. That is all the secret there is to RAII. Talking in code:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">template</div>
</li>
<li class="li1">
<div class="de1">class RAII_Wrapper <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;T *resource;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp;public:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; RAII_Wrapper <span class="br0">&#40;</span>T *resource<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : resource<span class="br0">&#40;</span>resource<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resource-&gt;open<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; ~RAII_Wrapper <span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resource-&gt;close<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<h3>An example</h3>
<p>Compare that to a visitor pattern. It&#8217;s just too simple to be of any use, isn&#8217;t it? Well, contrary to what Java fanboys tend to believe you can do lots of nice things without writing a bazillion lines of code.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">int</span> foo<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Expensive_Resource x;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; x.<span class="me1">open</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; try <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>not bar<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">-1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> catch <span class="br0">&#40;</span>&#8230;<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw <span class="st0">&quot;up&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> ret;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; try <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ret = baz<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>catch<span class="br0">&#40;</span>&#8230;<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// We don&#8217;t care, we&#8217;re closing x anyway</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; x.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> ret;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Wow&#8230; a whole lot of code just for calling bar and baz. And as I wrote that without even compiling I&#8217;m sure there are too many hidden bugs, lots of code-paths my simple programmer&#8217;s mind can&#8217;t even begin to imagine which will cause my Expensive_Resource to be leaked!</p>
<p>Let&#8217;s rewrite that using RAII:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">void</span> foo<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Expensive_Resource x;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; RAII_Wrapper release<span class="br0">&#40;</span>&amp;amp;x<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>not bar<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">return</span> <span class="nu0">-1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> baz<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>A lot nicer, isn&#8217;t it? But, what happened to all the try/catch if&#8217;s and closes there?</p>
<h3>Where&#8217;s the magic?</h3>
<p>The magic of RAII lies in how C++ handles exceptions. When we have a built object (<a href="http://nicolasb.com.ar/2010/02/c-composite-objects-and-throwing-constructors">can an object be in an unbuilt state?</a>) it means it&#8217;s constructor has correctly ran. It also means it&#8217;s destructor will run when it goes out of scope, doesn&#8217;t mater HOW it goes out of scope.</p>
<p>See how brilant that is? Doesn&#8217;t matter if a function we&#8217;re calling throws, or if we need to return before reaching the end of the function: the destructor will be called and thus our Expensive_Resource will be free!</p>
<h3>Why is this C++ specific?</h3>
<p>This is an easy one: think how would you implement this in Java: right, you can&#8217;t. Not knowing when is your object going to be destroyed means you can&#8217;t do anything useful in its destructor, therefore RAII is deeply rooted within the memory management in C++ and it&#8217;s pretty much a language specific pattern (or is it?). But, is that so good?</p>
<h3>Not everything is so great&#8230;</h3>
<p>You are probably thinking this is the best discovery since ice cream was invented. Well, not so fast, RAII has it&#8217;s detractors too.</p>
<p>The first problem in RAII, it doesn&#8217;t have a graceful way of handling resource acquisition failure. If Expensive_Resource is a database, and it&#8217;s connection fails, we have <a href="http://nicolasb.com.ar/2010/02/c-composite-objects-and-throwing-constructors">a throwing constructor</a>.</p>
<p>Even if throwing constructors are acceptable, throwing destructors may even be a worst idea: throwing while an exception is already active is a cause for concern (tip: it&#8217;ll crash your application, doesn&#8217;t matter how many try/catch blocks you use, due to stack unwinding issues).</p>
<p>And then, even if we don&#8217;t care about throwing destructors you have the issue of a release failure: how do you notify the user that a release failure has occurred? And what do you do, should it happen?</p>
<p>So, RAII is a great idea indeed, and it has it&#8217;s uses, but you should be careful when choosing this C++ specific pattern.</p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2010/07/design-patterns-c-idiom-raii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Template metaprogramming XI: Hidden Agenda</title>
		<link>http://nicolasb.com.ar/2010/07/template-metaprogramming-xi-hidden-agenda/</link>
		<comments>http://nicolasb.com.ar/2010/07/template-metaprogramming-xi-hidden-agenda/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 10:00:03 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Templates]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=918</guid>
		<description><![CDATA[


Wow, number eleven already. We&#8217;re getting more chapters here than Final Fantasy games. I didn&#8217;t even imagine there was so much to write about such an esoteric language features like templates. I do wonder if anyone will actually read it, but that&#8217;s a completly different problem.




Enough meta-meta talk: what can we do with all the [...]]]></description>
			<content:encoded><![CDATA[<table>
<tbody>
<tr>
<td>Wow, number eleven already. We&#8217;re getting more chapters here than Final Fantasy games. I didn&#8217;t even imagine there was so much to write about such an esoteric language features like templates. I do wonder if anyone will actually read it, but that&#8217;s a completly different problem.</td>
<td><a href="http://nicolasb.com.ar/archivos/2010/06/malboro.jpg"><img class="aligncenter size-thumbnail wp-image-995" title="malboro" src="http://nicolasb.com.ar/archivos/2010/06/malboro-150x150.jpg" alt="" width="150" height="150" /></a></td>
</tr>
</tbody>
</table>
<p>Enough meta-meta talk: what can we do with all the things we have learned? We can calculate pi and e, we already showed that as an example on one of the first chapters. This chapter I&#8217;m going to write about what motivated me to explore the bizarre underworld of template metaprogramming.  Some time ago I had to <a href="http://nicolasb.com.ar/?s=berkeley">work with a Berkeley DB</a> researching the feasibility of developing a magic cache for (real) DB table. Leaving aside the discussion of whether this is a good idea (the project did have a good reason to be researched) I hit a major roadblock when trying to provide a façde for every table; something like this:  <a href="http://nicolasb.com.ar/archivos/2010/06/virtualtemplate.png"><img class="aligncenter size-medium wp-image-994" title="virtualtemplate" src="http://nicolasb.com.ar/archivos/2010/06/virtualtemplate-300x145.png" alt="" width="300" height="145" /></a> See the problem? To do something like that we&#8217;d need a <a href="http://nicolasb.com.ar/2009/08/c-magic-callbacks-solved/">virtual template method</a>, and you can&#8217;t have that. After seeing that I thought to myself &#8220;Hey, I&#8217;ll use templates!&#8221;. Then I had two problems, but the damage was done, I couldn&#8217;t go back.  What kind of contorted device could we implement to make such a devious requirement work? I&#8217;ll leave you to think it, the answers I came up with next week.</p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2010/07/template-metaprogramming-xi-hidden-agenda/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ pretty functions</title>
		<link>http://nicolasb.com.ar/2010/06/c-pretty-functions/</link>
		<comments>http://nicolasb.com.ar/2010/06/c-pretty-functions/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 10:00:12 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=428</guid>
		<description><![CDATA[There are two well known macros from the preprocessor which every macro-sorcer must know. They are __FILE__ and __LINE__. You probably already know about them but anyway, __FILE__ will give you the current file and __LINE__ the current line. Easy, huh?







int main&#40;&#41; &#123;


&#160; &#160;printf&#40;&#34;%s : %i&#34;, __FILE__, __LINE__&#41;;


&#160; &#160;return 0;


&#125;








The program above would give you [...]]]></description>
			<content:encoded><![CDATA[<p>There are two well known macros from the preprocessor which every macro-sorcer must know. They are __FILE__ and __LINE__. You probably already know about them but anyway, __FILE__ will give you the current file and __LINE__ the current line. Easy, huh?</p>
<table>
<tbody>
<tr>
<td>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">&quot;%s : %i&quot;</span>, __FILE__, __LINE__<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
</td>
<td><a href="http://nicolasb.com.ar/archivos/2009/06/gccegg-65.png"><img class="aligncenter size-full wp-image-438" title="gccegg-65" src="http://nicolasb.com.ar/archivos/2009/06/gccegg-65.png" alt="gccegg-65" width="109" height="130" /></a></td>
</tr>
</tbody>
</table>
<p>The program above would give you &#8220;main.cpp : 3&#8243; as a result. There is nothing going on at execution time, it&#8217;s all preprocesor wizardy. In fact with &#8220;<strong>g{++/cc} -E</strong>&#8221; you can even check what the &#8220;real&#8221; output is (-E means to return the preprocessor output. Keep in mind a lot of stuff will be included from the headers you use).</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">&quot;%s : %i&quot;</span>, <span class="st0">&quot;main.cpp&quot;</span>, <span class="nu0">3</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Well that&#8217;s nice and all, but g++ can top this easily:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;std::<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; __PRETTY_FUNCTION__ &lt;&lt; <span class="st0">&quot;<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>There are a couple of notable things about this new &#8220;pretty function&#8221; thing:</p>
<ul>
<li>1. It will demangle a function&#8217;s name</li>
<li>2. This time it isn&#8217;t a preprocessor secret thing but a real variable g++ will create.</li>
</ul>
<p>You can easily use this for better logging functions now (with some macro wizardy, obviously).</p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2010/06/c-pretty-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Template metaprogramming X: Zero Minus Ten</title>
		<link>http://nicolasb.com.ar/2010/06/template-metaprogramming-x-zero-minus-ten/</link>
		<comments>http://nicolasb.com.ar/2010/06/template-metaprogramming-x-zero-minus-ten/#comments</comments>
		<pubDate>Thu, 17 Jun 2010 10:00:24 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[Templates]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=914</guid>
		<description><![CDATA[So far we&#8217;ve learned the basic constructs of template metaprogramming (loops, branching, return values) and some basic list operations (getting the length of a list, appending and preppending elements, checking if an element is included in a list). Let&#8217;s put it all together by creating an operation to return the position of an element. It&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>So far we&#8217;ve learned the basic constructs of template metaprogramming (loops, branching, return values) and some basic list operations (getting the length of a list, appending and preppending elements, checking if an element is included in a list). Let&#8217;s put it all together by creating an operation to return the position of an element. It&#8217;ll be very useful later on too.</p>
<p>If we go back to the Includes operation we can get some help to define the Position operation: the position of an element in a list is one plus the position of the element we&#8217;re searching for in the tail, or zero if the head equals said element. The operation is not defined if the element is not in the list.</p>
<p>Translating to pseudo-code:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Position (lst.head, lst) &lt;- 0
</div>
</li>
<li class="li1">
<div class="de1">Position (e, lst) &lt;- 1 + Position(e, lst.tail)</div>
</li>
</ol>
</div>
<p>The translation to C++ is not so trivial this time. Try it, I&#8217;ll wait&#8230; ready? OK, let&#8217;s start</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">template &lt;class Elm, class Lst&gt; <span class="kw4">struct</span> Position <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename Lst::<span class="me2">head</span> Head;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename Lst::<span class="me2">tail</span> Tail;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> bool found = <span class="br0">&#40;</span>Head == Elm<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> result = found? <span class="nu0">0</span> : <span class="nu0">1</span> + next;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> next = Position&lt;Elm, Tail&gt;::<span class="me2">result</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>Looks easy&#8230; but doesn&#8217;t work. First problem, we can&#8217;t compare two types, remember? We need to use Eq&lt;X, Y&gt; again. Second problem, although we said the operation is undefined if the element is not included on the list, it would be nice if we could force the compiler to fail if (or when) that happens. Let&#8217;s rewrite the operation using a façade again, but adding an Assert:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">template &lt;typename Elm, typename LST&gt; <span class="kw4">struct</span> _Position <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename LST::<span class="me2">head</span> Head;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename LST::<span class="me2">tail</span> Tail;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> bool found = Eq&lt;Elm, Head&gt;::<span class="me2">result</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> result = <span class="br0">&#40;</span>found<span class="br0">&#41;</span>? <span class="nu0">0</span> : <span class="nu0">1</span> + _Position&lt;Elm, Tail&gt;::<span class="me2">result</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">template &lt;typename Elm, typename LST&gt; <span class="kw4">struct</span> Position <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename Assert&lt;Includes&lt; Elm, LST &gt;::<span class="me2">result</span>&gt;::<span class="me2">check</span> include;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> result = _Position&lt;Elm, LST&gt;::<span class="me2">result</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>Oh, we haven&#8217;t defined assert yet! There&#8217;s another problem, too: even if it won&#8217;t compile, the compiler will try to expand _Position&lt; &#8230;, NIL &gt; indefinately, causing an error after too many nested template calls. Not nice. We need to add a case to make the compiler stop:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="coMULTI">/******************************************************/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Helper: Will fail to compile if the assert is false</span></div>
</li>
<li class="li1">
<div class="de1">class Assertion<span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">template &lt;bool cond, class T=Assertion&gt; <span class="kw4">struct</span> Assert <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename T::<span class="me2">fail</span> check;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">template &lt;&gt; <span class="kw4">struct</span> Assert&lt;true&gt; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> <span class="kw4">void</span> check;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/******************************************************/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">template &lt;typename Elm, typename LST&gt; <span class="kw4">struct</span> _Position <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename LST::<span class="me2">head</span> Head;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename LST::<span class="me2">tail</span> Tail;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> bool found = Eq&lt;Elm, Head&gt;::<span class="me2">result</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> result = <span class="br0">&#40;</span>found<span class="br0">&#41;</span>? <span class="nu0">0</span> : <span class="nu0">1</span> + _Position&lt;Elm, Tail&gt;::<span class="me2">result</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// The compiler will try to expand the position check</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// after NIL has been reached if this isn&#8217;t here</span></div>
</li>
<li class="li1">
<div class="de1">template &lt;typename Elm&gt; <span class="kw4">struct</span> _Position&lt;Elm, NIL&gt; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> result = <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">template &lt;typename Elm, typename LST&gt; <span class="kw4">struct</span> Position <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename Assert&lt;Includes&lt; Elm, LST &gt;::<span class="me2">result</span>&gt;::<span class="me2">check</span> include;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> result = _Position&lt;Elm, LST&gt;::<span class="me2">result</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>All that code for such a simple operation, man. Also, see what we did with Assert&lt;&gt;? It seems making a compile fail is actually quite easy. That&#8217;s what I have most experience with.</p>
<p>We&#8217;ve been through quite a lot, and our toolboox should be quite big already. Next time we&#8217;ll start steering towards some sort of aplicability, trying to use some of all these stuff to implement a real, useful and working program&#8230; assuming that&#8217;s even posible.</p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2010/06/template-metaprogramming-x-zero-minus-ten/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Template metaprogramming IX: Absolute Zero</title>
		<link>http://nicolasb.com.ar/2010/06/template-metaprogramming-ix-absolute-zero/</link>
		<comments>http://nicolasb.com.ar/2010/06/template-metaprogramming-ix-absolute-zero/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 10:00:59 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Templates]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=908</guid>
		<description><![CDATA[By now we should have learned how to perform loops, branching and returns using templates. Let&#8217;s add a couple of useful operations to our library: append and prepend.

Prepending an element to a list is very easy: the result is a list (oh surprise) consisting of a head (the element we want to add) and a [...]]]></description>
			<content:encoded><![CDATA[<p>By now we should have learned how to perform loops, branching and returns using templates. Let&#8217;s add a couple of useful operations to our library: append and prepend.</p>
<p><a href="http://nicolasb.com.ar/archivos/2010/06/novaAbsoluteZero_main_image.jpg"><img class="aligncenter size-medium wp-image-910" title="novaAbsoluteZero_main_image" src="http://nicolasb.com.ar/archivos/2010/06/novaAbsoluteZero_main_image-300x237.jpg" alt="" width="300" height="237" /></a></p>
<p>Prepending an element to a list is very easy: the result is a list (oh surprise) consisting of a head (the element we want to add) and a tail (the old list). In the pseudocode I&#8217;ve been using so far:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Prepend(e, lst) &lt;- LST(e, lst)</div>
</li>
</ol>
</div>
<p>And in C++ (trivial, this time):</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">template &lt;typename Elm, typename Lst=NIL&gt; <span class="kw4">struct</span> Preppend <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> LST&lt;Elm, Lst&gt; result;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>Appending is a little bit more difficult, as we need to first find the end of the list. Think for a second how would you define it&#8230; back? Ok, I&#8217;d define it this way: appending an element to the list yields a list, consisting of the same head and the result of appending said element to the tail. The null case, as usual, is appending an element to a NIL list; in this case the result is a list with the element itself. So:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Append(e, NIL) &lt;- LST(e)
</div>
</li>
<li class="li1">
<div class="de1">Append(e, lst) &lt;- LST(lst.head, Append(e, lst.tail))</div>
</li>
</ol>
</div>
<p>Looks complicated what it follows the same structure as the rest of the basic-ops:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">template &lt;class Elm, class Lst&gt; <span class="kw4">struct</span> Append <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename Lst::<span class="me2">head</span> Head;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename Lst::<span class="me2">tail</span> Tail;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename Append&lt;Elm, Tail&gt;::<span class="me2">result</span> Next;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename LST&lt;Head, Next&gt;::<span class="me2">result</span> result;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">template &lt;class Elm&gt; <span class="kw4">struct</span> Append&lt;Elm, NIL&gt; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> LST&lt;Elm&gt; result;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>Easy. Now, what happens if we want to add a default value for Lst, so we can use Append to create lists? Easy too, but we need a façade this time; just rename Append to _Append, then</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co1">// This is here just because I wanted a default param <img src='http://nicolasb.com.ar/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </span></div>
</li>
<li class="li1">
<div class="de1">template &lt;typename Elm, typename Lst=NIL&gt; <span class="kw4">struct</span> Append <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename _Append&lt;Elm, Lst&gt;::<span class="me2">result</span> result;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>I promised to add one more operation to our toolbox, returning the position of an element, but this post is getting quite long and I&#8217;m afraid it may be too much for the average attention span of a programmer&#8230; we&#8217;ll leave it for next time.</p>
<p><a href="http://nicolasb.com.ar/archivos/2010/02/vampires-nosferatu.jpg"><img class="aligncenter size-full wp-image-730" title="vampires-nosferatu" src="http://nicolasb.com.ar/archivos/2010/02/vampires-nosferatu.jpg" alt="" width="300" height="300" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2010/06/template-metaprogramming-ix-absolute-zero/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Template metaprogramming VIII: A Rough Whimper of Insanity</title>
		<link>http://nicolasb.com.ar/2010/06/template-metaprogramming-viii-a-rough-whimper-of-insanity/</link>
		<comments>http://nicolasb.com.ar/2010/06/template-metaprogramming-viii-a-rough-whimper-of-insanity/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 10:00:03 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Templates]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=902</guid>
		<description><![CDATA[Remember last time? We learned how to get the lenght of a list. This time I&#8217;ll introduce some more of these basic ops. Let&#8217;s begin with &#8220;Nth&#8221;: getting the Nth element of a list; which, remember, in this case is a type, not a concrete element. This means the Nth element will be something like [...]]]></description>
			<content:encoded><![CDATA[<p>Remember last time? We learned how to get the lenght of a list. This time I&#8217;ll introduce some more of these basic ops. Let&#8217;s begin with &#8220;Nth&#8221;: getting the Nth element of a list; which, remember, in this case is a type, not a concrete element. This means the Nth element will be something like int, char, const char*, not 1, 2 or 3. We introduced a trick to get around this limitation before using a template &lt;int&gt;, go there to refresh your memory if needed.</p>
<p><a href="http://nicolasb.com.ar/archivos/2010/04/manson1a.jpg"><img class="aligncenter size-full wp-image-903" title="manson1a" src="http://nicolasb.com.ar/archivos/2010/04/manson1a.jpg" alt="" width="270" height="290" /></a></p>
<p>So, what would the coloquial definition of &#8220;Nth&#8221; be? I&#8217;d put it like &#8220;The operation Nth for a list equals the head of the list for N = 0 and Nth (minus one) of tha tail otherwise&#8221;. A little bit more formally:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Nth(0, lst) &lt;- lst.head
</div>
</li>
<li class="li1">
<div class="de1">Nth(n, lst) &lt;- Nth(n-1, lst.tail)</div>
</li>
</ol>
</div>
<p>Translating this to C++ should be a breeze to you now. Try it, I&#8217;ll wait. Read? OK, this is MY answer:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">template &lt;typename LST, <span class="kw4">int</span> N&gt; <span class="kw4">struct</span> Nth <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename LST::<span class="me2">Tail</span> Tail;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename Nth&lt;Tail, N<span class="nu0">-1</span>&gt;::<span class="me2">result</span> result;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">template &lt;typename LST&gt; <span class="kw4">struct</span> Nth&lt;LST, <span class="nu0">0</span>&gt; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename LST::<span class="me2">head</span> result;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>Though the structure is very similar to the previous &#8220;basic operation&#8221;, getting the length of a list, the concept is quite different. This time we&#8217;re defining a return type recursivly. Anyway, it was too easy indeed, let&#8217;s try a more complex operation now.</p>
<p>How can we check if an element exists on a list? Seems easy enough, an element is included in a list if the head equals the element itself or if the element is included in the tail. In the pseudo language I just invented:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Includes(lst.head, lst) &lt;- true
</div>
</li>
<li class="li1">
<div class="de1">Includes(e, lst) &lt;- Includes(e, lst.tail)</div>
</li>
</ol>
</div>
<p>Looks easy, right? Well, there&#8217;s a bug there, can you spot it? Yeah, we&#8217;re missing the false condition. We should add a third spetialization:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Includes(lst.head, lst) &lt;- true
</div>
</li>
<li class="li1">
<div class="de1">Includes(e, NIL) &lt;- false
</div>
</li>
<li class="li1">
<div class="de1">Includes(e, lst) &lt;- Includes(e, lst.tail)</div>
</li>
</ol>
</div>
<p>Again, let&#8217;s translate the pseudocode to C++. Try it, I&#8217;ll wait. Read? OK, this is MY answer:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">template &lt;class Elm, class Lst&gt; <span class="kw4">struct</span> Includes <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename LST::<span class="me2">head</span> Head;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> typename LST::<span class="me2">tail</span> Tail;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> bool found = <span class="br0">&#40;</span>Elm == Head<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> bool found_tail = Includes&lt;Elm, Tail&gt;::<span class="me2">result</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> bool result = found || found_tail;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">template &lt;class Elm&gt; <span class="kw4">struct</span> Includes &lt;Elm, NIL&gt; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> bool result = <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>Looks nice, doesn&#8217;t it? Too bad it won&#8217;t work, you can&#8217;t compare two types. What would (int == char) mean in C++? We need a helper there, some kind of trick to compare to types. We can use partial template spetialization again:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">template &lt;class X, class Y&gt; <span class="kw4">struct</span> Eq <span class="br0">&#123;</span> <span class="kw4">static</span> <span class="kw4">const</span> bool result = <span class="kw2">false</span>; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">template &lt;class X&gt; <span class="kw4">struct</span> Eq&lt;X, X&gt; <span class="br0">&#123;</span> <span class="kw4">static</span> <span class="kw4">const</span> bool result = <span class="kw2">true</span>; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>With this little struct now we can write our include operation this way:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">template &lt;class Elm, class Lst&gt; <span class="kw4">struct</span> Includes <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> bool result =</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Eq&lt;Elm, typename LST::<span class="me2">head</span>&gt;::<span class="me2">result</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|| Includes&lt;Elm, typename LST::<span class="me2">tail</span>&gt;::<span class="me2">result</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">template &lt;class Elm&gt; <span class="kw4">struct</span> Includes&lt;Elm, NIL&gt; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">const</span> bool result = <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>Very esoteric looking, the right mix of Haskel, C++ and booze to ensure job security for life. Next time we&#8217;ll find a way to search for the position of an element, a somewhat more complicated task.</p>
<p><a href="http://nicolasb.com.ar/archivos/2010/02/animaniacs-8.jpg"><img class="aligncenter size-medium wp-image-707" title="animaniacs-8" src="http://nicolasb.com.ar/archivos/2010/02/animaniacs-8-300x225.jpg" alt="" width="300" height="225" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2010/06/template-metaprogramming-viii-a-rough-whimper-of-insanity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
