<?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>ejohansson.se &#187; c++</title>
	<atom:link href="http://ejohansson.se/tags/cpp/feed/" rel="self" type="application/rss+xml" />
	<link>http://ejohansson.se</link>
	<description>Random bits about Linux, programming, computers and occasionally other stuff.</description>
	<lastBuildDate>Sun, 18 Apr 2010 20:23:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>C++Next</title>
		<link>http://ejohansson.se/archives/2009/10/30/cpp-next/</link>
		<comments>http://ejohansson.se/archives/2009/10/30/cpp-next/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 19:41:46 +0000</pubDate>
		<dc:creator>Erik Johansson</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[c++]]></category>

		<guid isPermaLink="false">http://ejohansson.se/?p=290</guid>
		<description><![CDATA[Found a very interesting C++ site the other day: C++Next. I highly recommend it if you&#8217;re interested in &#8220;the next generation of C++&#8221;. 
]]></description>
			<content:encoded><![CDATA[<p>Found a very interesting C++ site the other day: <a href="http://cpp-next.com/">C++Next</a>. I highly recommend it if you&#8217;re interested in &#8220;the next generation of C++&#8221;. </p>
]]></content:encoded>
			<wfw:commentRss>http://ejohansson.se/archives/2009/10/30/cpp-next/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using booleans to document&#160;code</title>
		<link>http://ejohansson.se/archives/2006/04/09/using-booleans-to-document-code/</link>
		<comments>http://ejohansson.se/archives/2006/04/09/using-booleans-to-document-code/#comments</comments>
		<pubDate>Sun, 09 Apr 2006 16:12:09 +0000</pubDate>
		<dc:creator>Erik Johansson</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[qt]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://ejohansson.se/archives/2006/04/09/using-booleans-to-document-code/</guid>
		<description><![CDATA[Today I want to share a tip on how to write self-documenting code that I picked up from the book Code Complete 2 by Steve McConnell. I will do this be showing you some code I just wrote, but let us start with the actual tip.

Instead of merely testing a boolean expression, you can assign [...]]]></description>
			<content:encoded><![CDATA[<p>Today I want to share a tip on how to write self-documenting code that I picked up from the book <a href="http://www.amazon.com/gp/product/0735619670/">Code Complete 2</a> by Steve McConnell. I will do this be showing you some code I just wrote, but let us start with the actual tip.</p>
<blockquote cite="http://www.cc2e.com/" title="Steve McConnell"><p>
Instead of merely testing a boolean expression, you can assign the expression to a variable that makes the implication of the test unmistakable.
</p></blockquote>
<p>The following code was my first try, not employing the tip.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp-qt" style="font-family:monospace;"><span style="color: #0057AE;">void</span> CETabWidget<span style="color: #006E28;">::</span><span style="color: #2B74C7;">wheelEvent</span><span style="color: #006E28;">&#40;</span><span style="color: #22aadd;">QWheelEvent</span> <span style="color: #006E28;">*</span>e<span style="color: #006E28;">&#41;</span>
<span style="color: #006E28;">&#123;</span>
  <span style="color: #22aadd;">QTabBar</span> <span style="color: #006E28;">*</span>tabs <span style="color: #006E28;">=</span> tabBar<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
  <span style="color: #000000; font-weight:bold;">if</span> <span style="color: #006E28;">&#40;</span>e<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">y</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">&lt;</span> tabs<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">y</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">||</span> e<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">y</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">&gt;</span> <span style="color: #006E28;">&#40;</span>tabs<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">y</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">+</span> tabs.<span style="color: #2B74C7;">height</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">&#123;</span>
    e<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">ignore</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
    return<span style="color: #006E28;">;</span>
  <span style="color: #006E28;">&#125;</span>
&nbsp;
  <span style="color: #888888;">// handle wheel event</span>
<span style="color: #006E28;">&#125;</span></pre></div></div>

<p>Even though I wrote the code myself, I have problems understanding it. And making sure it&#8217;s correct takes more than a quick glance. If you want to understand this code in six months, you have to add a comment. So let us take a look at the same code rewritten to use two extra booleans.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp-qt" style="font-family:monospace;"><span style="color: #0057AE;">void</span> CETabWidget<span style="color: #006E28;">::</span><span style="color: #2B74C7;">wheelEvent</span><span style="color: #006E28;">&#40;</span><span style="color: #22aadd;">QWheelEvent</span> <span style="color: #006E28;">*</span>e<span style="color: #006E28;">&#41;</span>
<span style="color: #006E28;">&#123;</span>
  <span style="color: #22aadd;">QTabBar</span> <span style="color: #006E28;">*</span>tabs <span style="color: #006E28;">=</span> tabBar<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
  <span style="color: #0057AE;">const</span> <span style="color: #0057AE;">bool</span> cursorAboveTabBar <span style="color: #006E28;">=</span> <span style="color: #006E28;">&#40;</span>e<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">y</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">&lt;</span> tabs<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">y</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
  <span style="color: #0057AE;">const</span> <span style="color: #0057AE;">bool</span> cursorBelowTabBar <span style="color: #006E28;">=</span> <span style="color: #006E28;">&#40;</span>e<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">y</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">&gt;</span> <span style="color: #006E28;">&#40;</span>tabs<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">y</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">+</span> tabs<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">height</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
  <span style="color: #000000; font-weight:bold;">if</span> <span style="color: #006E28;">&#40;</span>cursorAboveTabBar <span style="color: #006E28;">||</span> cursorBelowTabBar<span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">&#123;</span>
    e<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">ignore</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
    return<span style="color: #006E28;">;</span>
  <span style="color: #006E28;">&#125;</span>
&nbsp;
  <span style="color: #888888;">// handle wheel event</span>
<span style="color: #006E28;">&#125;</span></pre></div></div>

<p>Now you can quickly figure out that the wheel event is only handled if the cursor is inside the tabBar. And it&#8217;s actually possible to take a quick look and be sure that the code is doing what it&#8217;s supposed to.</p>
<p>Of course, for this to be of any good, you need to give the boolean variables meaningful names. Naming them a and b wouldn&#8217;t help a bit.</p>
<p>The best thing with this approach? You don&#8217;t have to write comments and still get code that people can understand. Ah, the joy!</p>
]]></content:encoded>
			<wfw:commentRss>http://ejohansson.se/archives/2006/04/09/using-booleans-to-document-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Odd (and deprecated) g++&#160;extensions</title>
		<link>http://ejohansson.se/archives/2006/03/28/odd-and-deprecated-g-extensions/</link>
		<comments>http://ejohansson.se/archives/2006/03/28/odd-and-deprecated-g-extensions/#comments</comments>
		<pubDate>Tue, 28 Mar 2006 22:23:27 +0000</pubDate>
		<dc:creator>Erik Johansson</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[c++]]></category>

		<guid isPermaLink="false">http://ejohansson.se/archives/2006/03/28/odd-and-deprecated-g-extensions/</guid>
		<description><![CDATA[I&#8217;ve been spending the evening reading about libstdc++ (mostly the documentation) and how to do stuff The Right Way (the documentation is actually interesting, you should at least take a look at it). Now I feel the need to share some stuff that&#8217;s a bit more odd then what you see in the documentation.
First: &#8220;the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been spending the evening reading about <a href="http://gcc.gnu.org/libstdc++/">libstdc++</a> (mostly the <a href="http://gcc.gnu.org/onlinedocs/libstdc++/documentation.html">documentation</a>) and how to do stuff The Right Way (the documentation is actually interesting, you should at least take a look at it). Now I feel the need to share some stuff that&#8217;s a bit more odd then what you see in the documentation.</p>
<p>First: <a href="http://docs.freebsd.org/info/gcc/gcc.info.Naming_Results.html">&#8220;the named return value extension&#8221;</a>. The idea is to avoid creating a temporary variable that&#8217;s assigned a value and then returned by value, thus creating an unnecessary copy. A small example:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">std<span style="color: #008080;">::</span><span style="color: #007788;">list</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> foo<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> j<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> l<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#123;</span>
  l.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>j<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This example will create just one list and append j to that. Without this extension a local list needs to be created, j appended to that and the copy constructor invoked when returning. This extension is deprecated in g++ 3.3 and removed in g++ 4.0.</p>
<p>My other example is the <a href="http://docs.freebsd.org/info/gcc/gcc.info.Min_and_Max.html">&#8220;min and max operators&#8221;</a>. No, this is not about min and max macros nor the std::min and std::max functions. This is the min and max <strong>operators</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> min <span style="color: #339933;">=</span> x <span style="color: #339933;">&lt;?</span> y<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> max <span style="color: #339933;">=</span> x <span style="color: #339933;">&gt;?</span> y<span style="color: #339933;">;</span></pre></div></div>

<p>There is of course also a short-hand version for assigning one of the variables the smallest (or biggest) value of the two.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">x <span style="color: #339933;">&lt;?=</span> y<span style="color: #339933;">;</span>
x <span style="color: #339933;">&gt;?=</span> y<span style="color: #339933;">;</span></pre></div></div>

<p>They are all deprecated in g++ 4.0.</p>
]]></content:encoded>
			<wfw:commentRss>http://ejohansson.se/archives/2006/03/28/odd-and-deprecated-g-extensions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Reclaiming memory with&#160;templates</title>
		<link>http://ejohansson.se/archives/2006/02/24/reclaiming-memory-with-templates/</link>
		<comments>http://ejohansson.se/archives/2006/02/24/reclaiming-memory-with-templates/#comments</comments>
		<pubDate>Fri, 24 Feb 2006 01:05:39 +0000</pubDate>
		<dc:creator>Erik Johansson</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[c++]]></category>

		<guid isPermaLink="false">http://ejohansson.se/archives/2006/02/24/reclaiming-memory-with-templates-and-for_each/</guid>
		<description><![CDATA[Most of the time when using STL containers in C++, you want to use them with pointers to preserve run-time polymorphic behaviour. For example, a list of strings would be defined as

std::list&#60;std::string&#62; myStrings;

The only problem with this is that you need to remember to call delete on every element in that list before myStrings goes [...]]]></description>
			<content:encoded><![CDATA[<p>Most of the time when using STL containers in C++, you want to use them with pointers to preserve run-time polymorphic behaviour. For example, a list of strings would be defined as</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">std<span style="color: #008080;">::</span><span style="color: #007788;">list</span><span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000080;">&gt;</span> myStrings<span style="color: #008080;">;</span></pre></div></div>

<p>The only problem with this is that you need to remember to call delete on every element in that list before myStrings goes out of scope. That&#8217;s not a big problem you may argue, just iterate trough the whole list and delete them.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">std<span style="color: #008080;">::</span><span style="color: #007788;">list</span><span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> it <span style="color: #000080;">=</span> myStrings.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
std<span style="color: #008080;">::</span><span style="color: #007788;">list</span><span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> end <span style="color: #000080;">=</span> myStrings.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>it <span style="color: #000040;">!</span><span style="color: #000080;">=</span> end<span style="color: #008000;">&#41;</span> <span style="color: #0000dd;">delete</span> <span style="color: #000040;">*</span>it<span style="color: #000040;">++</span><span style="color: #008080;">;</span></pre></div></div>

<p>But when you have many containers with pointers, this duplication of code tends to become tedious. This is where templates and for_each comes into play. Add the following code to a header filer.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">/// A function with the sole purpose of deleting @a ptr</span>
<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">void</span> deletePtr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> ptr<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">delete</span> ptr<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">/// Calls delete on every element in @a container</span>
<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">void</span> deleteAllPtr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> container<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  std<span style="color: #008080;">::</span><span style="color: #007788;">for_each</span><span style="color: #008000;">&#40;</span>container.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, container.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,
                deletePtr<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #008080;">::</span><span style="color: #007788;">value_type</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Then, when you wish to delete a container with pointers, just include the header file and call</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">deleteAllPtr<span style="color: #008000;">&#40;</span>myStrings<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Simply beautiful <img src='http://ejohansson.se/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ejohansson.se/archives/2006/02/24/reclaiming-memory-with-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
