<?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, 20 Dec 2011 09:00:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Anonymous objects in C++</title>
		<link>http://nicolasb.com.ar/2011/12/anonymous-objects-in-c/</link>
		<comments>http://nicolasb.com.ar/2011/12/anonymous-objects-in-c/#comments</comments>
		<pubDate>Tue, 20 Dec 2011 09:00:54 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=1584</guid>
		<description><![CDATA[There are not many cases in which you can have an anonymous *anything* in your code, yet there&#8217;s an idiom in C++ which lets you use an object with an anonymous type. Like this: void foo() { struct { int x; }; } Why would this be useful, you may ask. That&#8217;s a valid question. [...]]]></description>
			<content:encoded><![CDATA[<p>There are not many cases in which you can have an anonymous *anything* in your code, yet there&#8217;s an idiom in C++ which lets you use an object with an anonymous type. Like this:</p>
<pre>void foo()
{
   struct {
      int x;
   };
}</pre>
<p>Why would this be useful, you may ask. That&#8217;s a valid question. This idiom can be very useful to write callbacks, like this:</p>
<pre>struct Interface {
   void callback() = 0;
};

void bar(Interface &amp;c);

void foo()
{
   struct : public Interface {
      /* ... */
   } x;

   bar(x);
}</pre>
<p>I am not aware of many other uses for an anonymous type. Even more considering this idiom can now be replaced with a much cleaner lambda. But hey, it looks cool!</p>
<p style="text-align: center;"><a href="http://nicolasb.com.ar/archivos/2011/08/anonymous.jpg"><img class="aligncenter size-full wp-image-1590" title="anonymous" src="http://nicolasb.com.ar/archivos/2011/08/anonymous.jpg" alt="" width="240" height="238" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2011/12/anonymous-objects-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++: Checking if a method exists in a parent class</title>
		<link>http://nicolasb.com.ar/2011/12/c-checking-if-a-method-exists-in-a-parent-class/</link>
		<comments>http://nicolasb.com.ar/2011/12/c-checking-if-a-method-exists-in-a-parent-class/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 09:00:56 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=1194</guid>
		<description><![CDATA[I like Google test and Google mock (C++ only) a lot. These are really great tools to ensure the quality of your code. They do have one problem however, especially when working with a legacy codebase: many times you need to change a signature for a function and your tests begin to fail. Those tests [...]]]></description>
			<content:encoded><![CDATA[<p>I like <a href="http://nicolasb.com.ar/2009/06/testing-mocking-c/">Google test and Google mock (C++ only)</a> a lot. These are really great tools to ensure the quality of your code. They do have one problem however, especially <a href="http://nicolasb.com.ar/2009/10/retrofitting-unit-tests-for-legacy-code/">when working with a legacy codebase</a>: many times you need to change a signature for a function and your tests begin to fail. Those tests shouldn&#8217;t really fail, they shouldn&#8217;t compile at all because you&#8217;re now trying to mock a function which doesn&#8217;t exists anymore.</p>
<p>I worked on a patch to check if a class and its parent share the same methods, but I hit some major roadblocks which I believe cannot be saved:</p>
<ul>
<li>You have no way of detecting if the parent&#8217;s method is actually virtual. It may have the same signature, yet if it isn&#8217;t virtual the mock serves no purpose</li>
<li>You have no (easy) way of detecting if the method is defined in your parent&#8217;s parent</li>
</ul>
<p>Even though this code will never be useful, I thought I might as well post this here, just in case anyone comes up with a solution for those problems.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co1">// :!g++ foo.cpp -lgtest_main -lgmock &amp;amp;&amp;amp; ./a.out</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;gtest/gtest.h&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;gmock/gmock.h&gt;</span></div>
</li>
<li class="li1">
<div class="de1">using namespace testing;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* &quot;Real&quot; application</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">class Foo <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; public:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; virtual <span class="kw4">int</span> bar<span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">return</span> <span class="nu0">1</span>; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//virtual int bar(void) { return 1; }</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">class <span class="kw1">Do</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> something<span class="br0">&#40;</span>Foo &amp;amp;foo<span class="br0">&#41;</span><span class="br0">&#123;</span> <span class="kw1">return</span> foo.<span class="me1">bar</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>; <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="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Class used to compare method ptrs. We need to inherit from class C</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* to forward the calls to the derived methods.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">template &lt;class C, class D&gt;</div>
</li>
<li class="li2">
<div class="de2">class Mocks_Must_Exist_In : public C <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// We don&#8217;t know in the derived class the typeof the parent class</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// so we define a common name using some template magic</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> C ParentClass;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Actually we don&#8217;t know our own class either</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> D Self;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Function ptr definitions are ugly so we might as well use</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// a template to hide it under the rug</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; template &lt;class F, class G&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">void</span> mock_created_for_unexisting_method<span class="br0">&#40;</span>F f, G g<span class="br0">&#41;</span><span class="br0">&#123;</span> f = g; <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="li1">
<div class="de1"><span class="co2">#define METHOD_EXISTS(Method) \</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">void</span> defined_<span class="co2">##Method() { \</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mock_created_for_unexisting_method<span class="br0">&#40;</span>&amp;amp;Self::<span class="me2">Method</span>, &amp;amp;ParentClass::<span class="me2">Method</span><span class="br0">&#41;</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="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp;* Checked mock, shouldn&#8217;t compile if Foo&#8217;s interface changes</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">class FooMock : public Mocks_Must_Exist_In&lt; Foo, FooMock &gt; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; MOCK_METHOD1<span class="br0">&#40;</span>bar, <span class="kw4">int</span><span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; METHOD_EXISTS<span class="br0">&#40;</span>bar<span class="br0">&#41;</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"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Unchecked mock, should compile if Foo&#8217;s interface changes</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">class FooMock2 <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; MOCK_METHOD0<span class="br0">&#40;</span>bar, <span class="kw4">int</span><span class="br0">&#40;</span><span class="br0">&#41;</span><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>
<li class="li1">
<div class="de1">TEST<span class="br0">&#40;</span>FooTest, ThisShouldCompile<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; FooMock foo;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; EXPECT_CALL<span class="br0">&#40;</span>foo, bar<span class="br0">&#40;</span>_<span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">WillOnce</span><span class="br0">&#40;</span><span class="kw1">Return</span><span class="br0">&#40;</span><span class="nu0">42</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; EXPECT_EQ<span class="br0">&#40;</span><span class="nu0">42</span>, <span class="kw1">Do</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">something</span><span class="br0">&#40;</span>foo<span class="br0">&#41;</span><span class="br0">&#41;</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>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2011/12/c-checking-if-a-method-exists-in-a-parent-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ name mangling</title>
		<link>http://nicolasb.com.ar/2011/11/c-name-mangling/</link>
		<comments>http://nicolasb.com.ar/2011/11/c-name-mangling/#comments</comments>
		<pubDate>Thu, 17 Nov 2011 07:00:32 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=798</guid>
		<description><![CDATA[There is a topic I have referred to several times on this blog, yet in four years I still haven&#8217;t explained what it is. I plan to correct this by explaining a little bit about C++ name mangling, and although I won&#8217;t expect to write anything you couldn&#8217;t learn by reading Wikipedia, I&#8217;ll try to [...]]]></description>
			<content:encoded><![CDATA[<p>There is a topic I have referred to several times on this blog, yet in four years I still haven&#8217;t explained what it is. I plan to correct this by explaining a little bit about C++ name mangling, and although I won&#8217;t expect to write anything you couldn&#8217;t learn by reading <a href="http://en.wikipedia.org/wiki/Name_mangling">Wikipedia</a>, I&#8217;ll try to have a more practical approach.</p>
<p>Whenever you compile and link something, there is a lot of information the compiler deduces that you don&#8217;t really care about. Things like calling conventions, overloads or namespaces. Yet this information is crucial for other stages of the compiler (or linker) to work. For this reason, the compiler will create a decorated version of any object&#8217;s or function&#8217;s name.</p>
<p>In its most simple case, it would be something like this:</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">void</span> overloaded_function<span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">void</span> overloaded_function<span class="br0">&#40;</span><span class="kw4">string</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Which would then be translated to something like:</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">void</span> fastcall_int_overloaded_function<span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">void</span> fastcall_string_overloaded_function<span class="br0">&#40;</span><span class="kw4">string</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Of course, for more complex functions (like class methods) the mangling is much more complicated. Also, remember that&#8217;s just a mangling convention I just invented, and most likely not used by any compiler in existence.</p>
<p>Although for the most part we can just ignore name mangling, this has a couple of consequences of which we should be aware:</p>
<h3>Creating a name for anonymous objects/functions</h3>
<p>I will not explain much about this, it might be the topic of another post, but there are certain cases in which you can have a struct or a function defined inside another object anonymously. In these cases, the mangler will assign some sort of denomination for this anonymous object.</p>
<h3>Linking with C symbols</h3>
<p>C has no mangling. It just doesn&#8217;t need it. This has a very important consequence, whenever you use C code in C++ you need to specify that your doing so, by using an extern &#8220;C&#8221; declaration.</p>
<h3>Debugging</h3>
<p>gdb already takes care of this so it may be transparent to you, but if you are using a debugger not aware of how your compiler mangles names, you may end up with a lot of very difficult to understand names.</p>
<h3>Bonus: Demangling C++ names</h3>
<p>If you find yourself in the last case, for example when running an nm to get the names defined in a (compiled) object, you can use c++ filt. Like this:</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">nm foo.<span class="me1">o</span> | c++filt</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2011/11/c-name-mangling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cool C++0X features XV: Initializer lists for custom (non-std) objects</title>
		<link>http://nicolasb.com.ar/2011/10/cool-c0x-features-xv-initializer-lists-for-custom-non-std-objects/</link>
		<comments>http://nicolasb.com.ar/2011/10/cool-c0x-features-xv-initializer-lists-for-custom-non-std-objects/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 10:00:38 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++0x]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=1431</guid>
		<description><![CDATA[Last time we saw how you can use C style array-initialization for C++ objects, like this: &#160; &#160; &#160; &#160; std::vector&#60;int&#62; v = &#123;1,2,3,4&#125;; We also saw this works for may types of objects, like maps and pairs. How about custom developed objects? Yes, that&#8217;s right, you can have initilizer lists for your own objects [...]]]></description>
			<content:encoded><![CDATA[<p>Last time we saw how you can use C style array-initialization for C++ objects, like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; std::<span class="me2">vector</span>&lt;int&gt; v = <span class="br0">&#123;</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span>,<span class="nu0">4</span><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>We also saw this works for may types of objects, like maps and pairs. How about custom developed objects? Yes, that&#8217;s right, you can have initilizer lists for your own objects too, and it&#8217;s quite easy. C++0x offers initializer_lists which you can use on your constructors (or any other methods, for that mater) to have C-style initialization. Let&#8217;s see an example. We already know how to sum a list of numbers using template lists and variadic templates, so let&#8217;s try adding an initializer consisting of numbers:</p>
<p>Let&#8217;s start by creating a class which can accept an initializer list:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;initializer_list&gt;</span></div>
</li>
<li class="li1">
<div class="de1">using namespace std;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">struct</span> Add_List <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; Add_List<span class="br0">&#40;</span>initializer_list&lt;int&gt; 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="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="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; &nbsp; &nbsp; Add_List x = <span class="br0">&#123;</span><span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &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>That&#8217;s interesting, as you can see an initializer list is actualy a template class, meaning that nested initializers can easily be defined too. Now, we have the interface, how can we access each element of the list? Let&#8217;s do the same thing I did when I found out about initilizers, let&#8217;s search for the header file.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"> &nbsp;template&lt;class _E&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; class initializer_list</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public:</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> _E &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; value_type;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> <span class="kw4">const</span> _E&amp;amp; &nbsp; &nbsp; reference;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> <span class="kw4">const</span> _E&amp;amp; &nbsp; &nbsp; const_reference;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> size_t &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; size_type;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> <span class="kw4">const</span> _E* &nbsp; &nbsp; &nbsp; &nbsp; iterator;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="kw4">typedef</span> <span class="kw4">const</span> _E* &nbsp; &nbsp; &nbsp; &nbsp; const_iterator;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; private:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; iterator&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _M_array;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; size_type&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _M_len;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// The compiler can call a private constructor.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; initializer_list<span class="br0">&#40;</span>const_iterator __a, size_type __l<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; : _M_array<span class="br0">&#40;</span>__a<span class="br0">&#41;</span>, _M_len<span class="br0">&#40;</span>__l<span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#125;</span></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; initializer_list<span class="br0">&#40;</span><span class="br0">&#41;</span> : _M_array<span class="br0">&#40;</span><span class="kw2">NULL</span><span class="br0">&#41;</span>, _M_len<span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span> <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">&nbsp; &nbsp; &nbsp; <span class="co1">// Number of elements.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; size_type</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; size<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> <span class="br0">&#123;</span> <span class="kw1">return</span> _M_len; <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; <span class="co1">// First element.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; const_iterator</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; begin<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> <span class="br0">&#123;</span> <span class="kw1">return</span> _M_array; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// One past the last element.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; const_iterator</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; end<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> <span class="br0">&#123;</span> <span class="kw1">return</span> begin<span class="br0">&#40;</span><span class="br0">&#41;</span> + size<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>Looks surprisingly easy (note that this is for G++ 4.something only). And it is, most of the magic happens in the compiler, so the userland code is quite straight forward. According to that header file, we could build something like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;iostream&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;initializer_list&gt;</span></div>
</li>
<li class="li1">
<div class="de1">using namespace std;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw4">struct</span> Add_List <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Add_List<span class="br0">&#40;</span>initializer_list&lt;int&gt; lst<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="kw4">int</span> sum = <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw4">auto</span> i = lst.<span class="me1">begin</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; i != lst.<span class="me1">end</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; ++i<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; sum += *i;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; sum &lt;&lt; <span class="st0">&quot;<span class="es0">\n</span>&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"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><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; &nbsp; &nbsp; Add_List x = <span class="br0">&#123;</span><span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &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>As you can see, the initializer lists can be used in any place an iterable container is required, as long as it&#8217;s const. There&#8217;s not much more magic to it, but we can use some more C++0x devices to make our list-adding device much more cool, for example to support different actions and not only addition. Next time, though.</p>
<p>PS: An important lesson from this article: don&#8217;t be afraid to look into the system headers, they won&#8217;t bite. You should never ever change them, but taking a peek can only improve your C++ knowledge.</p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2011/10/cool-c0x-features-xv-initializer-lists-for-custom-non-std-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cool C++0X features XIV: Initializer lists</title>
		<link>http://nicolasb.com.ar/2011/10/cool-c0x-features-xiv-initializer-lists/</link>
		<comments>http://nicolasb.com.ar/2011/10/cool-c0x-features-xiv-initializer-lists/#comments</comments>
		<pubDate>Tue, 11 Oct 2011 10:00:22 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++0x]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=1427</guid>
		<description><![CDATA[We talked last time about ranged fors and how they can simplify our life in C++0x. Now we are going to take a trip back to old C land. Remember when you could initialize your arrays like this: int v&#91;&#93; = &#123;1, 2, 3, 4&#125;; C++ brought a lot of changes to the world, and [...]]]></description>
			<content:encoded><![CDATA[<p>We talked last time about ranged fors and how they can simplify our life in C++0x. Now we are going to take a trip back to old C land. Remember when you could initialize your arrays like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">int</span> v<span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="br0">&#123;</span><span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span>, <span class="nu0">4</span><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>C++ brought a lot of changes to the world, and suddenly instead of int[] you were supposed to use vector, and with it your initializer didn&#8217;t work anymore. Though luck. Try to compile this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;vector&gt;</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; &nbsp; &nbsp; std::<span class="me2">vector</span>&lt;int&gt; v = <span class="br0">&#123;</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span>,<span class="nu0">4</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &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>
</ol>
</div>
<p>If you did compile it with g++, you may have noticed an interesting error message:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">error: in C+<span class="nu0">+98</span> ‘v’ must be initialized by constructor, not by ‘<span class="br0">&#123;</span>&#8230;<span class="br0">&#125;</span>’</div>
</li>
<li class="li1">
<div class="de1">warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x</div>
</li>
</ol>
</div>
<p>That&#8217;s interesting. Try to compile it with g++ again, but using C++0x instead of plain C++. Magic, now it works!</p>
<p>Initializers lists bring the best of C to C++ world (?) by letting you use initialize any object with an initializer. And I mean *any* object, not just vectors. For example, say you have a map (a map and a bunch of other stuff):</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; &nbsp; &nbsp; map&lt;string, vector&lt;int&gt;&gt; v = <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; <span class="br0">&#123;</span> <span class="st0">&quot;a&quot;</span>, <span class="br0">&#123;</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span><span class="br0">&#125;</span> <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> <span class="st0">&quot;b&quot;</span>, <span class="br0">&#123;</span><span class="nu0">4</span>,<span class="nu0">5</span>,<span class="nu0">6</span><span class="br0">&#125;</span> <span class="br0">&#125;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> <span class="st0">&quot;c&quot;</span>, <span class="br0">&#123;</span><span class="nu0">7</span>,<span class="nu0">8</span>,<span class="nu0">9</span><span class="br0">&#125;</span> <span class="br0">&#125;</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="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; v<span class="br0">&#91;</span><span class="st0">&quot;b&quot;</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> &lt;&lt; <span class="st0">&quot;<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &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>
</ol>
</div>
<p>Yes, that works! Maps, vectors, pairs, and even your own custom objects, but we&#8217;ll see that next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2011/10/cool-c0x-features-xiv-initializer-lists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cool C++0X features XII: type inference with auto</title>
		<link>http://nicolasb.com.ar/2011/10/cool-c0x-features-xii-type-inference-with-auto/</link>
		<comments>http://nicolasb.com.ar/2011/10/cool-c0x-features-xii-type-inference-with-auto/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 10:00:17 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++0x]]></category>
		<category><![CDATA[Templates]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=1399</guid>
		<description><![CDATA[In the last four entries we worked on a simple example, like the one I&#8217;m pasting below, of type inference with decltype, which led us to learn about delayed type declaration and decltypes with auto. This time I want to focus just on the auto keyword instead. template &#60;class&#8230; Args&#62; auto wrap&#40;Args&#8230; a&#41; -&#62; decltype&#40; [...]]]></description>
			<content:encoded><![CDATA[<p>In the last four entries we worked on a simple example, like the one I&#8217;m pasting below, of type inference with decltype, which led us to learn about <a href="http://nicolasb.com.ar/2011/06/cool-c0x-features-ix-delayed-type-declaration">delayed type declaration</a> and <a href="http://nicolasb.com.ar/2011/06/cool-c0x-features-x-type-inference-with-decltype">decltypes with auto</a>. This time I want to focus just on the auto keyword instead.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">template &lt;class&#8230; <span class="me1">Args</span>&gt;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">auto</span> wrap<span class="br0">&#40;</span>Args&#8230; <span class="me1">a</span><span class="br0">&#41;</span> -&gt; decltype<span class="br0">&#40;</span> do_something<span class="br0">&#40;</span>a&#8230;<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; 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; &nbsp; &nbsp; <span class="kw1">return</span> do_something<span class="br0">&#40;</span>a&#8230;<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>We saw <a href="http://nicolasb.com.ar/2011/06/cool-c0x-features-x-type-inference-with-decltype">last time</a> how decltype can be used in a contrived way to create a local variable without specifying its type, only how to deduce the type for this variable. Luckily, that verbose method of type declaration can be summed up in the following way:</p>
<table width="100%">
<tbody>
<tr>
<td>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> x = <span class="nu0">2</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> y = <span class="nu0">3</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; decltype<span class="br0">&#40;</span>x*y<span class="br0">&#41;</span> z = x*y;</div>
</li>
</ol>
</div>
</td>
<td>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> x = <span class="nu0">2</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> y = <span class="nu0">3</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">auto</span> z = x*y;</div>
</li>
</ol>
</div>
</td>
</tr>
</tbody>
</table>
<p>That&#8217;s right, when you are declaring local variables it&#8217;s easier and cleaner to just use auto. This feature isn&#8217;t even &#8220;in the wild&#8221; yet, so you can&#8217;t really predict what will people do with it, but it seems to me that limiting its use to local variables with a very short lived scope is the best strategy. We are yet to see what monstrosities the abuse of this feature will produce, and I&#8217;m sure there will be many. Regardless of their potential to drive insane any maintainers, its best use probably comes in loops.</p>
<p>In any C++ application, you&#8217;ll find code like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span>FooContainer&lt;Bar&gt;::<span class="me2">const_iterator</span> i = foobar.<span class="me1">begin</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; i != foobar.<span class="me1">end</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; ++i<span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>This ugly code can be eliminated with something much more elegant:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw4">auto</span> i = foobar.<span class="me1">begin</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; i != foobar.<span class="me1">end</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; ++i<span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>Looks nicer indeed, but we can improve it much further with other tools. We&#8217;ll see how the next time. For the time being, let&#8217;s see for waht what auto is not to be used.</p>
<p>When using auto, keep in mind it was designed to simplify the declaration of a variable with a complex or difficult to reason type, not as a replacement for other language features like templates. This is a common mistake:</p>
<table width="100%">
<tbody>
<tr>
<td>Wrong</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">void</span> f<span class="br0">&#40;</span><span class="kw4">auto</span> x<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; x;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
</td>
<td>Right</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">template &lt;T&gt;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">void</span> f<span class="br0">&#40;</span>T x<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; x;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
</td>
</tr>
</tbody>
</table>
<p>It makes no sense to use auto in the place of a template, since a template means that the type will be completed later whereas auto means it should be deduced from an initializer.</p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2011/10/cool-c0x-features-xii-type-inference-with-auto/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cool C++0X features XI: decltype and disappearing constness</title>
		<link>http://nicolasb.com.ar/2011/09/cool-c0x-features-xi-decltype-and-disappearing-constness/</link>
		<comments>http://nicolasb.com.ar/2011/09/cool-c0x-features-xi-decltype-and-disappearing-constness/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 10:00:15 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++0x]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=1446</guid>
		<description><![CDATA[After a long, long hiatus, the C++0x series are back. You may want to check where we left by reading the last posts of this series. In the last few entries we saw how to use decltype for type inference. Object types is a problem that seems easy but gets complicated very quickly, for example [...]]]></description>
			<content:encoded><![CDATA[<p>After a long, long hiatus, the C++0x series are back. You may want to check where we left by reading the <a href="http://nicolasb.com.ar/category/programming/c/c0x/">last posts</a> of this series.</p>
<p>In the last few entries we saw how to use decltype for type inference. Object types is a problem that seems easy but gets complicated very quickly, for example when you start dealing with constness. Constness is difficult in many ways but this time I want to review how constness works with type inference. This topic is not C++0x specific as it&#8217;s present for template type deduction too, but decltype adds a new level of complexity to it.</p>
<p>Let&#8217;s start with an example. Would this 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; &nbsp; &nbsp; <span class="kw4">int</span> bar;</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="kw4">void</span> f<span class="br0">&#40;</span><span class="kw4">const</span> Foo foo<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; foo.<span class="me1">bar</span> = <span class="nu0">42</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Clearly not, having a const Foo means you can&#8217;t touch foo.bar. How about this?</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; &nbsp; &nbsp; <span class="kw4">int</span> bar;</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="kw4">void</span> f<span class="br0">&#40;</span><span class="kw4">const</span> Foo foo<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; int&amp;amp; x = foo.<span class="me1">bar</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>That won&#8217;t compile either, you can&#8217;t initialize an int reference from a const int, yet we can do this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">void</span> f<span class="br0">&#40;</span><span class="kw4">const</span> Foo foo<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">const</span> int&amp;amp; x = foo.<span class="me1">bar</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>If we know that works it must mean that s.result&#8217;s type is const int. Right? Depends.</p>
<p>Just as the name implies decltype yields the declared type of a variable, and what&#8217;s the declared type for Foo.bar?</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; &nbsp; &nbsp; <span class="kw4">int</span> bar;</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="kw4">void</span> f<span class="br0">&#40;</span><span class="kw4">const</span> Foo foo<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// This won&#8217;t compile</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; int&amp;amp; x = foo.<span class="me1">bar</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// This will</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; decltype<span class="br0">&#40;</span>foo.<span class="me1">bar</span><span class="br0">&#41;</span> x = <span class="nu0">42</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>That&#8217;s an interesting difference, but it makes sense once you are used to it. To make things more interesting, what happens if you start adding parenthesis (almost) randomly? Try to deduce the type of x:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">void</span> f<span class="br0">&#40;</span><span class="kw4">const</span> Foo foo<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; decltype<span class="br0">&#40;</span><span class="br0">&#40;</span>foo.<span class="me1">bar</span><span class="br0">&#41;</span><span class="br0">&#41;</span> x</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>If <strong>decltype(x)</strong> is the type of <strong>x</strong> then <strong>decltype((foo.bar))</strong> is the type of <strong>(foo.bar)</strong>. Between <strong>foo.bar</strong> and <strong>(foo.bar)</strong> there&#8217;s a very important difference; the first refers to a variable whilst the last refers to an expression. Even though <strong>foo.bar</strong> was declared as int, the expression <strong>(foo.bar)</strong> will yield a const int&amp;, since that&#8217;s the type (though implicit and not declared, since the expression is not declared).</p>
<p>This is how we would complete the example then:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">void</span> f<span class="br0">&#40;</span><span class="kw4">const</span> Foo foo<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// This two statements are equivalent</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; decltype<span class="br0">&#40;</span><span class="br0">&#40;</span>foo.<span class="me1">bar</span><span class="br0">&#41;</span><span class="br0">&#41;</span> x = <span class="nu0">42</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">const</span> int&amp;amp; y = <span class="nu0">42</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// It&#8217;s very easy to confirm that the typeof x is now const int&amp;amp;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// This won&#8217;t compile:</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; x = <span class="nu0">24</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>As I said, disappearing constness is not a C++0x specific problem as it may occur on template type deduction, but that&#8217;s besides the point of this post. Next time we&#8217;ll continue working with type deduction, but with the new auto feature this time.</p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2011/09/cool-c0x-features-xi-decltype-and-disappearing-constness/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Throwing destructors</title>
		<link>http://nicolasb.com.ar/2011/09/throwing-destructors/</link>
		<comments>http://nicolasb.com.ar/2011/09/throwing-destructors/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 10:00:44 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=832</guid>
		<description><![CDATA[We already know what happens when you throw from a constructor. Ending up with a half built object is not good, but suppose we do manage to build one correctly. What happens if we throw in a destructor instead? The results are usually much worse, with a very real possibility of having your program terminated. [...]]]></description>
			<content:encoded><![CDATA[<p>We already know what happens when you <a href="http://nicolasb.com.ar/2010/02/c-composite-objects-and-throwing-constructors/">throw from a constructor</a>. Ending up with a half built object is not good, but suppose we do manage to build one correctly. What happens if we <a href="http://nicolasb.com.ar/2010/07/design-patterns-c-idiom-raii/">throw in a destructor</a> instead? The results are usually much worse, with a very real possibility of having your program terminated. Read on for a brief explanation on the perils of throwing constructors.</p>
<p>So, according to RAII pattern, resource deallocation should occur during the destructor, yet resource freeing is not exempt of possible errors. How would you notify of an error condition?</p>
<ul>
<ul>
<li>First error handling choice, you notify /dev/null of the error condition. Best case, you may log the error somewhere, but you can&#8217;t do anything about it, you end up ignoring it. Not good, usually you&#8217;ll want to do something about the error condition, even more if it&#8217;s transient.</li>
<li>Second choice, throw. The user (of the class) will know something has gone horribly wrong. This option seems better, yet it has some disadvantages too (just as it happened with <a href="http://nicolasb.com.ar/2010/02/c-composite-objects-and-throwing-constructors/">throwing destructors</a>; when is an object completely deleted? is it ever deleted if an exception is thrown whilst running?)</li>
</ul>
</ul>
<p><a href="http://nicolasb.com.ar/archivos/2010/08/divide_by_zero.jpg"><img class="aligncenter size-medium wp-image-1213" title="divide_by_zero" src="http://nicolasb.com.ar/archivos/2010/08/divide_by_zero-300x240.jpg" alt="" width="300" height="240" /></a></p>
<p>Yet the worst part is not resource leaking through half destroyed objects, the worst part is having your program call std::abort.</p>
<p>Think of it this way: when an exception is active, the stack is unwind, i.e. the call stack is traversed backwards until a function which can handle the exception is found. And you just can&#8217;t unwind the stack while unwinding the stack (you&#8217;d need a stack of stacks) so the reasonable thing to do is call std::abort.</p>
<p>So, what can you do about it? Go to your favorite jobs posting site and start searching for a PHP position, you&#8217;ll sleep better at nights.</p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2011/09/throwing-destructors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Living on a null object</title>
		<link>http://nicolasb.com.ar/2011/08/living-on-a-null-object/</link>
		<comments>http://nicolasb.com.ar/2011/08/living-on-a-null-object/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 10:00:26 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=1291</guid>
		<description><![CDATA[Check this out: struct S &#123; &#160; &#160;int f&#40;&#41;&#123; return 42; &#125; &#125;; &#160; int main&#40;&#41; &#123; &#160; &#160;S *x = &#40;S*&#41; NULL; &#160; &#160;return x-&#62;f&#40;&#41;; &#125; What does this do? Does it compile? Does it crash? I&#8217;ll give you a second. Ready? It does compile, OK But it doesn&#8217;t crash. Why, you may ask [...]]]></description>
			<content:encoded><![CDATA[<p>Check this out:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">struct</span> S <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw4">int</span> f<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span> <span class="kw1">return</span> <span class="nu0">42</span>; <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="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;S *x = <span class="br0">&#40;</span>S*<span class="br0">&#41;</span> <span class="kw2">NULL</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">return</span> x-&gt;f<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>What does this do? Does it compile? Does it crash? I&#8217;ll give you a second.</p>
<p><a href="http://nicolasb.com.ar/archivos/2011/08/Homer-Simpson-Nuke-Plant.jpg"><img class="aligncenter size-full wp-image-1554" title="Homer-Simpson-Nuke-Plant" src="http://nicolasb.com.ar/archivos/2011/08/Homer-Simpson-Nuke-Plant.jpg" alt="" width="320" height="238" /></a></p>
<p>Ready? It does compile, OK<br />
But it doesn&#8217;t crash.<br />
Why, you may ask<br />
Think about it, you must.</p>
<p>The compiler will mangle S::f and translate this into something like:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">struct</span> S <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="kw4">int</span> mangled_S_f<span class="br0">&#40;</span><span class="kw4">struct</span> S *this<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">return</span> <span class="nu0">42</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="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;S *x = <span class="br0">&#40;</span>S*<span class="br0">&#41;</span> <span class="kw2">NULL</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;mangled_S_f<span class="br0">&#40;</span>x<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Now, in this new &#8220;translated&#8221; code, what do you think? Will it crash? It won&#8217;t, since no one is going to dereference &#8220;this&#8221;. Crazy, huh? This crazy idiom also allows even crazier things, like <a href="http://nicolasb.com.ar/2011/04/newsflash-c-object-commits-sepuku/">C++ objects committing sepuku</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2011/08/living-on-a-null-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Final classes in C++</title>
		<link>http://nicolasb.com.ar/2011/07/final-classes-in-c/</link>
		<comments>http://nicolasb.com.ar/2011/07/final-classes-in-c/#comments</comments>
		<pubDate>Tue, 05 Jul 2011 10:00:41 +0000</pubDate>
		<dc:creator>nico</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://nicolasb.com.ar/?p=1200</guid>
		<description><![CDATA[Have you ever wondered what&#8217;s the best way of having a class from which you can&#8217;t inherit, say, like Java&#8217;s final? Without any doubt, the best way is having a team capable of not doing things like inheriting from &#8216;class NeverEverEverInheritFromThis&#8217;. The second best way involves some magic and lots of beer: class Final &#123; [...]]]></description>
			<content:encoded><![CDATA[<table>
<tbody>
<tr>
<td>Have you ever wondered what&#8217;s the best way of having a class from which you can&#8217;t inherit, say, like Java&#8217;s final? Without any doubt, the best way is having a team capable of not doing things like inheriting from &#8216;class NeverEverEverInheritFromThis&#8217;. The second best way involves some magic and lots of beer:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">class Final <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; protected:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Final<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</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/cpp.jpg"><img class="aligncenter size-medium wp-image-402" title="cpp" src="http://nicolasb.com.ar/archivos/2009/06/cpp-225x300.jpg" alt="You'll need it" width="173" height="230" /></a></td>
</tr>
</tbody>
</table>
<p>So, what the hell does that evil device do? Easy, it defines a protected constructor, meaning only derived classes will be able to access it (i.e. no public construction of this object). How does this stop other classes from inheriting? It doesn&#8217;t, unless we add one more keyword:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">class Final <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; protected:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Final<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><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">class X : virtual Final <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>The virtual inheritance is meant to be used to avoid the dreaded diamond in multiple inheritance designs. It does a lot of magic with the constructors and the memory layout of the object; amongst other things, it&#8217;ll make any class which derives from X have only a single base class for Final and it&#8217;ll also make this hypothetical class call Final&#8217;s constructor without going through X first.</p>
<p>A complete explanation of virtual inheritance is beyond the scope of this article, but it&#8217;s enough for our Final device to know that it forces the virtual base&#8217;s constructors to be called first, thus now we can write this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">class Final <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; protected:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Final<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><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">class X : virtual Final <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">class Y : public X <span class="br0">&#123;</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="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; X x;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Y y;</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>
</ol>
</div>
<p>Try it and watch it fail!</p>
<p><a href="http://nicolasb.com.ar/archivos/2009/07/cpp.jpg"><img class="aligncenter size-medium wp-image-492" title="cpp" src="http://nicolasb.com.ar/archivos/2009/07/cpp-255x300.jpg" alt="" width="255" height="300" /></a></p>
<p><b>Update 2011-07-08:</b> Amazing how time flies. This article has been written about a year before its publishing, and, believe it or not, it&#8217;s already showing its age. What I would update on this article is the first paragraph: the best way of not having a problem with final classes is creating a design which doesn&#8217;t have artificial restrictions to the growth and extensibility of the system (i.e: don&#8217;t use final classes, they are usually a bad idea). I like that idea, I may write another article about it.</p>
]]></content:encoded>
			<wfw:commentRss>http://nicolasb.com.ar/2011/07/final-classes-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

