<?xml version="1.0" encoding="utf-8"?>
        <?xml-stylesheet type="text/css" href="http://blog.copton.net/styles/feed.css"?>
<rss version="2.0"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:admin="http://webns.net/mvcb/"
>
<channel>
<title>copton.net</title>
<link>http://blog.copton.net</link>
<description>2.4263102175e-12</description>
<dc:language>en-us</dc:language>
<dc:creator>Alexander Bernauer</dc:creator>
<dc:date>2010-03-03T22:30:55+01:00</dc:date>
<admin:generatorAgent rdf:resource="http://nanoblogger.sourceforge.net" />
<item>
<link>http://blog.copton.net/archives/2010/03/03/when_new_features_collide_with_old_syntax/index.html</link>
<title>When new features collide with old syntax</title>
<dc:date>2010-03-03T22:19:43+01:00</dc:date>
<dc:creator>Alexander Bernauer</dc:creator>
<dc:subject>C++</dc:subject>
<description><![CDATA[<p>My C++ times are over. But Roker keeps on feeding me with input. And I could not let this one go by unnoticed.</p>

<p>To make it short, what do you think does the following program print?
<blockquote>
<code>
<pre>
#include &lt;iostream&gt;

struct X { };

class Y {
public:
    Y (const X& x) {
        std::cout &lt;&lt; "fnord" &lt;&lt; std::endl;
    }
};

int main() {
    Y o(X());
    return 0;
}
</pre>
</code>
</blockquote>
</p>

<p>At first sight it looks like everything is ok. We know that references to temporaries must be <code>const</code>, which is the case here. So we think, <code>o</code> is going to be a <code>Y</code> object which was initilized with a temporary <code>X</code> object. In this case, the program should output "fnord".</code>

<p>But it doesn't. Instead it prints nothing. Mysterious you think? So did I. So I started debugging the issue while Roker was giggling. So, if <code>o</code> is not a <code>Y</code> object, what is it then? Let's ask the compiler and the C++ runtime:
<blockquote>
<code>
<pre>
#include &lt;typeinfo&gt;
//...
int main() {
    Y o(X());
    std::cout << typeid(o).name() << std::endl;
    return 0;
}
</pre>
</code>
</blockquote>
</p>

<p>The program now prints "F1YPF1XvEE". Hmm, I can not decrypt this, can you? We need to demangle the name in order to see it's real C++ nature:
<blockquote>
<code>
<pre>
$ c++filt -t F1YPF1XvEE
Y ()(X (*)())
</pre>
</code>
</blockquote>
</p>

<p>Who can decrypt this? If you can't, here is a <a href="http://unixwiz.net/techtips/reading-cdecl.html">howto on reading C type declarations</a>, because the unfamiliar syntax for function types is a C heritage. And what does this type mean after all? It means, that <code>d</code> is a pointer to a function which returns a <code>Y</code> object and expects a pointer to a function which returns a <code>X</code> object and expects nothing. Got it? If not, here is how I would write it, in case I would need such a type:
<blockquote>
<code>
<pre>
#include &lt;iostream&gt;
#include &lt;typeinfo&gt;

struct X { };
class Y { /*...*/ }; 

typedef X (Inner)();
typedef Y (Outer)(Inner);

int main()
{
    Outer o;
    std::cout &lt;&lt; typeid(o).name() &lt;&lt; std::endl;
    return 0;
}
</pre>
</code>
</blockquote>
This program prints <code>Y ()(X (*)())</code> which is the type of <code>o</code> from the original program.</p>

<p>
Ok, so now we know, what the program does. It instantiates a variable of type pointer to function whatever. That's why there is no <code>X</code> object ever created. So, although the code could mean what we originally intended, the compiler chooses to understand something else, which is also possible due to the C syntax rules. And what's the C way of removing ambiguity from syntax? Right, it's braces:
<blockquote>
<code>
<pre>
//...
int main()
{
    Y o((X()));
    std::cout &lt;&lt; typeid(o).name() &lt;&lt; std::endl;
    return 0;
}
</pre>
</code>
</blockquote>
Now, the program prints "fnord" and "1Y" which is the mangled name for the type <code>Y</code>.
</p>

<p>To sum it up, this is yet another example for why I consider the C heritage to be a major source of trouble for C++ developers. The C syntax was never designed for temporary variables and combining both yields to what we have seen here: ambiguity, ugly workarounds and surprise and furstration for the developer. Don't get me wrong, though. I still think that C++ could hardly have been made better given the design goals which where taken those days. I truly have a love-hate for that language.</p>]]></description>
</item>
<item>
<link>http://blog.copton.net/archives/2009/11/09/c_pitfalls/index.html</link>
<title>C++ Pitfalls </title>
<dc:date>2009-11-09T11:27:42+01:00</dc:date>
<dc:creator>Alexander Bernauer</dc:creator>
<dc:subject>C++</dc:subject>
<description><![CDATA[<p>As I do no C++ hacking at ETH I have not stumbled upon new input for this
blog on my own. So I am very happy that Roker keeps on feeding me.</p>

<p>Today he pointed me at <a href="http://www.horstmann.com/">Cay Horstmann</a>'s 
list of <a href="http://www.horstmann.com/cpp/pitfalls.html">C++ pitfalls</a>. 
Some of the issues have already been covered here but some haven't. I recommend
reading it to every C++ developer as this takes less time than learning it the
hard way by debugging code. Have fun.</p>

<p>
Concerning the iterator issues Cay provides a 
<a href="http://www.horstmann.com/safestl.html">Safe STL</a> implementation which
is supposed to be used as a drop in replacement for the STL during development.
It adds state and knowledge about the owner to iterators and uses this
information to do runtime checks. If you accidently use your iterators in a
wrong way you end up with a comprehensive runtime error instead of a segfault
"deep in the bowels of STL code". </p>

<p>
I haven't tested Safe STL. If anybody did feel free to send me your experience.
I would be glad to blog about it.
</p>]]></description>
</item>
<item>
<link>http://blog.copton.net/archives/2009/11/09/catching_integer_overflows_in_c/index.html</link>
<title>Catching Integer Overflows in C</title>
<dc:date>2009-11-09T10:06:03+01:00</dc:date>
<dc:creator>Alexander Bernauer</dc:creator>
<dc:subject>C++</dc:subject>
<description><![CDATA[Roker pointed me to an article about <a href="http://www.fefe.de/intof.html">Catching Integer Overflows in C</a> by <a href="http://www.fefe.de/">Felix von Leitner</a>. It covers some nasty pitfalls of integer arithmetic in C. My absolute favourite is that <code>abs</code> may return a negative number. Comforting to know that C++ inherited this behaviour....]]></description>
</item>
<item>
<link>http://blog.copton.net/archives/2009/08/22/the_safe_bool_idiom/index.html</link>
<title>The Safe Bool Idiom</title>
<dc:date>2009-08-22T11:48:14+01:00</dc:date>
<dc:creator>Alexander Bernauer</dc:creator>
<dc:subject>C++</dc:subject>
<description><![CDATA[<p>
Operator overloading is considered a major C++ feature. But as so often with
C++ this feature sometimes clashes with other language properties. 
</p>

<p>
Roker pointed me to an article about 
<a href="http://www.artima.com/cppsource/safebool.html">the safe bool idiom</a>
where the author elaborates on why a simple <code>operator bool</code> is not
satisfying if you want to be able to use an object within a bool context. Have
fun...
</p>]]></description>
</item>
<item>
<link>http://blog.copton.net/archives/2009/07/09/feature_collision/index.html</link>
<title>feature collision</title>
<dc:date>2009-07-09T15:05:35+01:00</dc:date>
<dc:creator>Alexander Bernauer</dc:creator>
<dc:subject>C++</dc:subject>
<description><![CDATA[<p>One thing the C++ community is pretty proud of is that, in contrast to C, C++
supports <a
href="http://www.parashift.com/c++-faq-lite/const-correctness.html">const
correctness</a>. Turns out that const correctness clashes with STL
containers.</p>

<p>Today Roker pointed me to a poor guy from #c++@freenode with the following problem:
<pre>
<code>
#include &lt;iostream&gt;
#include &lt;list&gt;

typedef std::list&lt;int&gt; list_t;

list_t::const_iterator find_first_odd(const list_t &amp; list)
{
  for (list_t::const_iterator i = list.begin(); i != list.end(); ++i)
  {
    if (*i % 2 != 0) return i;
  }

  return list.end();
}

int main()
{
  list_t list;
  list.push_back(2);
  list.push_back(3);
  list.push_back(4);

  list_t::const_iterator odd_i = find_first_odd(list);

  if (odd_i != list.end())
    // Error. Can not use const_iterator with 'std::list::erase'
    list.erase(odd_i);

  return 0;
}
</code>
</pre>
</p>

<p>
For the sake of const correctness the function <code>find_first_odd</code>
expects a constant reference to the list because it is not going to modify it.
But as a result the function can only return a <code>const_iterator</code>,
because there is no way to get a non-const <code>iterator</code> from the
constant list.
</p>

<p>
So the caller of <code>find_first_odd</code> ends up with a
<code>const_iterator</code> which is useless for modifying operations such as
<code>erase</code>. And for him it is also impossible to turn the
<code>const_iterator</code> into a non-const <code>iterator</code>. The two
types have no relation to each other, so casting does not work. And there are
no conversion functions in the STL either. So the only way out here is to
sacrifice const correctness and pass a non-const reference to
<code>find_first_odd</code>.
</p>

<p>
According to Roker nobody on #c++ had a better solution. And according to <a
href="http://www.aristeia.com/">Scott Meyers</a>' article <a
href="http://www.ddj.com/cpp/184401406">Three Guidelines for Effective Iterator
Usage</a> there is none. <code>const_iterator</code>s can not be easily transformed
into <code>iterator</code>s. What you can do is nasty and rather inefficient.
</p>

<p>Here it comes:
<pre>
<code>
template &lt;class Container&gt;
typename Container::iterator convert(typename Container::const_iterator in, Container&amp; container)
{
    typename Container::iterator out(container.begin());
    std::advance(out, std::distance&lt;typename Container::const_iterator&gt;(out, in));
    return out;
}
</code>
</pre>
If the container does not support random access, this conversion is O(n).
Furthermore it can only be done, if the container object is at hand. Finally
note, that this function violates const correctness, because the container object
which is not altered by the function is not passed as <code>const&amp;</code>.
The reason is that the <code>begin()</code> function of a constant container
returns a <code>const_iterator</code> which you can not convert into the output
non-const <code>iterator</code>. In case you are confused, try <a
href="http://upload.wikimedia.org/wikipedia/en/e/e8/Escher_Waterfall.jpg">this illustration</a>.
</p>]]></description>
</item>
<item>
<link>http://blog.copton.net/archives/2009/06/22/c_truths/index.html</link>
<title>C++ truths</title>
<dc:date>2009-06-22T14:23:57+01:00</dc:date>
<dc:creator>Alexander Bernauer</dc:creator>
<dc:subject>C++</dc:subject>
<description><![CDATA[<p>Since I am at the <a href="http://www.ethz.ch">ETH in Zurich</a> I haven't
used C++. The reason is simply the fact that I haven't encountered any problem
for which C++ is the tool of choice. That's why there has been almost no input
to this category from me.</p>

<p>But there are others who blog about C++. So I will be satisfied with
passively following the C++ topic and refering to them.</p>

<p>Today's featured C++ blog is <a href="http://cpptruths.blogspot.com/">C++
thruths</a> from Sumant Tambe. Hmm, infinite regress, yummy. ;-)</p>

<p>Thank you Roker</p>]]></description>
</item>
<item>
<link>http://blog.copton.net/archives/2009/05/20/the_longest_suicide_note_in_history/index.html</link>
<title>the longest suicide note in history</title>
<dc:date>2009-05-20T17:22:35+01:00</dc:date>
<dc:creator>Alexander Bernauer</dc:creator>
<dc:subject>C++</dc:subject>
<description><![CDATA[<p>I am not alone with my estimation of C++ being a dead end. Today Roker
pointed me to an <a
href="http://allankelly.blogspot.com/2009/04/accu-confernece-future-of-c.html">article
from Allan Kelly about the future of C++</a>. Here is my favourite quote:
<blockquote>
[the C++ 0x standard will be] the longest suicide note in history.
</blockquote>
</p>

<p>
Other languages do better. <a
href="http://www.python.org/download/releases/3.0/">Python 3.0</a> and <a
href="http://dev.perl.org/perl6/">Perl 6</a> are not downward compatible and in
both cases I think this is a good idea. Why is this impossible for C++?
</p>]]></description>
</item>
<item>
<link>http://blog.copton.net/archives/2009/03/30/git/index.html</link>
<title>git</title>
<dc:date>2009-03-30T10:05:27+01:00</dc:date>
<dc:creator>Alexander Bernauer</dc:creator>
<dc:subject>Vortrag</dc:subject>
<description><![CDATA[<p>
<a href="http://www.downgra.de">Rico</a> und ich haben beim <a
href="http://ulm.ccc.de/Rheintal">Chaostreff Rheintal</a> und dem <a
href="http://lab.netculture.at/dornbirn">net culture lab Dornbirn</a> einen
Vortrag über das Versionsverwaltungssystem <a href="http://git-scm.com">git</a>
gehalten.
</p>

<p>
Links zu den Folien und den Aufzeichnungen gibt es <a href="http://ulm.ccc.de/Rheintal">hier</a>.
</p>]]></description>
</item>
<item>
<link>http://blog.copton.net/archives/2009/02/20/moving_on/index.html</link>
<title>moving on</title>
<dc:date>2009-02-20T14:30:00+01:00</dc:date>
<dc:creator>Alexander Bernauer</dc:creator>

<description><![CDATA[<p>
I am a PhD student at <a href="http://www.ethz.ch/">ETH Zurich</a> now. Besides
other cool infrastructure the ETH provides blogs for associates. No doubt, I
had to try out. 
</p>

<p>
It's a <a href="http://mu.wordpress.org/">WordPress</a>, so I am leaving the
nanoblogger paradigm of static page content. What I can get is interaction
through reader comments and trackbacks. But I'm also a target for spam and
crackers now. We'll see...
</p>

<p>
Anyway, here is <a href="http://blogs.ethz.ch/copton/">copton's ETH blog</a>.
For the time being new posts will go there.
</p>]]></description>
</item>
<item>
<link>http://blog.copton.net/archives/2009/02/15/c_traps/index.html</link>
<title>C Traps</title>
<dc:date>2009-02-15T13:54:46+01:00</dc:date>
<dc:creator>Alexander Bernauer</dc:creator>
<dc:subject>C++</dc:subject>
<description><![CDATA[<p>Quite a time ago Andrew König has written a <a href="http://en.wikipedia.org/wiki/C_Traps_and_Pitfalls">book about C traps and pitfalls</a>. Today, Roker pointed me on an <a href="http://literateprogramming.com/ctraps.pdf">earlier technical report</a> which was the basis of the book.</p>

<p>Although some things are different with C++, I recommend everybody to read at least the report. Otherwise you surely will stumble on those items sooner or later.</p>]]></description>
</item>
</channel>
</rss>
