<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Pizer's Weblog</title>
	<atom:link href="http://pizer.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://pizer.wordpress.com</link>
	<description>programming, DSP, math</description>
	<lastBuildDate>Mon, 08 Feb 2010 20:17:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='pizer.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Pizer's Weblog</title>
		<link>http://pizer.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://pizer.wordpress.com/osd.xml" title="Pizer&#039;s Weblog" />
	<atom:link rel='hub' href='http://pizer.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Fast Digital Sine Oscillator</title>
		<link>http://pizer.wordpress.com/2010/02/08/fast-digital-sine-oscillator/</link>
		<comments>http://pizer.wordpress.com/2010/02/08/fast-digital-sine-oscillator/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 15:06:29 +0000</pubDate>
		<dc:creator>pizer</dc:creator>
				<category><![CDATA[DSP]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[fast]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[oscillator]]></category>
		<category><![CDATA[sine]]></category>

		<guid isPermaLink="false">http://pizer.wordpress.com/?p=512</guid>
		<description><![CDATA[I originally intended this blog to be about math and digital signal processing as well and not just about C++. Since I recently answered a programming forum question regarding efficient generation of sine waves and took the time to code one in &#8212; surprize &#8212; C++, I thought to myself, why not sharing the code [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=512&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I originally intended this blog to be about math and digital signal processing as well and not just about C++. Since I recently answered a programming forum question regarding efficient generation of sine waves and took the time to code one in &#8212; surprize &#8212; C++, I thought to myself, why not sharing the code along with explanations about how it works.</p>
<p>If you&#8217;re interested in software synthesizers in general or generating digital representations of DTMF tones and don&#8217;t want to waste many CPU cycles when generating sine waves, keep on reading.</p>
<p><span id="more-512"></span></p>
<p>Generating sampled sine waves without invoking the math library&#8217;s sin function for every sample is actually fairly simple. I will show two approaches of doing that with differing pros and cons and finally a version that combines the advantages of both.</p>
<p>Approach1: Imagine the 2D plane, a unit circle, and a point on that circle that rotates around the origin (see below). If you take the y-coordinate of that point, send this to the soundcard as one sample, rotate the point by an angle of <em>a</em> and repeat these steps you&#8217;ll hear a sine wave with a frequency of a*fs/(2*pi) Hz where fs is the sampling rate in Hertz. We only need to compute the rotation matrix in advance depending on the desired frequency. After that we need 4 multiplications and two additions per sample. Not so bad. But it&#8217;s also not really that fast.</p>
<p style="text-align:center;">
<img src="http://pizer.files.wordpress.com/2010/02/sine1.png?w=700" alt="circle" />
</p>
<p>Since we are only interested in one component of this point coordinate but we&#8217;re computing the other one as well, one might ask &#8220;Can we improve this and make it faster?&#8221;. And yes, there&#8217;s a way to do it.</p>
<p>Approach2: Instead of rotating the point by computing the product of a rotation matrix and a vector we can write the next point as a linear combination of the previous two points, see the picture below.</p>
<p style="text-align:center;">
<img src="http://pizer.files.wordpress.com/2010/02/sine21.png?w=700" alt="circle" />
</p>
<p>In this example, <em>p2</em> can be computed using <em>p2 = q + (q-p0) = 2 q &#8211; p0</em> where the point <em>q</em> is simply a scaled version of <em>p1</em>, namely <em>q = cos(a) p1</em>. If we put this together, we&#8217;ll arrive at the following linear recurrence equation</p>
<p style="text-align:center;">
<img src='http://s0.wp.com/latex.php?latex=p_n+%3D+2+cos%28a%29+p_%7Bn-1%7D+-+p_%7Bn-2%7D&amp;bg=fff&amp;fg=1c1c1c&amp;s=0' alt='p_n = 2 cos(a) p_{n-1} - p_{n-2}' title='p_n = 2 cos(a) p_{n-1} - p_{n-2}' class='latex' />
</p>
<p>The nice thing about it is that to compute the y-coordinate of the new point we don&#8217;t need any previous x-coordinates. In addition, we saved a multiplication per component. The factor <em>2 cos(a)</em> can be computed in advance, so, we&#8217;re able to compute additional sine wave samples with only one multiplication and one addition per sample. Actually, this variant is an oscillating <a href="http://en.wikipedia.org/wiki/Infinite_impulse_response">IIR</a> filter with a pair of complex conjugated poles on the unit circle. Usually, this is something you would avoid because such filters are unstable or &#8220;self-oscillating&#8221; to use another term. But in this case we exploit this oscillation property.</p>
<p>Are we done yet? Almost. What if we want to reset the phase of the oscillator or change the frequency without resetting the phase? How is that supposed to work? Admittedly, this is not that easy if we only remember the last two samples and the weighting factor <em>2 cos(a)</em>. Also, we have to keep in mind that calculations with floating point numbers involves rounding errors. It is quite possible that the amplitude of the sine wave will increase or decrease due to rounding errors. So, once in a while we should normalize it. Normalization is also not that trivial if you rely on the recurrence equation alone.</p>
<p>I think the most elegant solution is a combination of both approaches. We can use the current point including x-coordinate and the rotation matrix as the oscillator&#8217;s state for easier phase, frequency and amplitude manipulations. But when the user is interested in generating many samples at a time, we unroll the loop to produce a block of samples using the linear recurrence equation. Also, instead of vectors and rotation matrices we can simply use <a href="http://en.wikipedia.org/wiki/Complex_number">complex numbers</a>. Finally, here&#8217;s the C++0x source code of my sine generator. To keep it short but still flexible, I made use of C++0x&#8217;s new lambda feature:</p>
<pre><code>#ifndef SINE_GENERATOR_HPP_INCLUDED
#define SINE_GENERATOR_HPP_INCLUDED

#include &lt;cmath&gt;
#include &lt;complex&gt;

class sine_generator
{
    std::complex&lt;double&gt; phase;
    std::complex&lt;double&gt; rotation;

    template&lt;typename Sink&gt;
    void internal_generate(int count, Sink sink);

public:
    sine_generator() : phase(1),rotation(1) {}

    void set_frequency(double rad)
    { rotation = std::polar(double(1),rad); }

    void reset_phase(double rad)
    { phase = std::polar(double(1),rad); }

    template&lt;typename OutputIter&gt;
    void generate(int count, OutputIter begin)
    {
        internal_generate(count,[&amp;](double smp){
            *begin = smp; ++begin;
        });
    }

    template&lt;typename ForwardIter, typename BinOp&gt;
    void generate(int count, ForwardIter begin, BinOp binop)
    {
        internal_generate(count,[&amp;](double smp){
           *begin = binop(*begin,smp); ++begin;
        });
    }
};

template&lt;typename Sink&gt;
void sine_generator::internal_generate(int count, Sink sink)
{
    std::complex&lt;double&gt; rot_forx = pow(rotation,12);
    phase /= abs(phase); // prevent drifting off due to rounding errors
    // use linear recurrence for generating the samples
    double const fy = 2 * real(rotation);
    double const fx = 2 * real(rot_forx);
    double y0 = imag(phase);
    double x0 = real(phase);
    double y1 = imag(phase*conj(rotation));
    double x1 = real(phase*conj(rot_forx));
    double tt; // temporary variable
    while (count &gt;= 12) {
        // 4*3 = 12 samples in a row ...
        for (int k=0; k&lt;4; ++k) {
            // output       | compute next sample
            sink(y0); tt = fy*y0-y1;
            sink(tt); y1 = fy*tt-y0;
            sink(y1); y0 = fy*y1-tt;
        }
        // update x as well ...
        tt = fx*x0-x1; x1 = x0; x0 = tt;
        count -= 12;
    }
    phase = std::complex&lt;double&gt;(x0,y0);
    while (count-- &gt; 0) {
        sink(imag(phase));
        phase *= rotation;
    }
}

#endif // SINE_GENERATOR_HPP_INCLUDED 

</code></pre>
<p>Here&#8217;s an example program that uses the sine generator:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;functional&gt;

#include "sine.hpp"

using std::vector;
using std::cout;
using std::plus;

int main()
{
    const int buffsize = 160;
    vector&lt;double&gt; buffer(buffsize);

    sine_generator sg;

    sg.set_frequency(440 * 2*3.141592653589793 / 8000);
    sg.reset_phase(0);
    // overwrite buffer with sine wave ...
    sg.generate(buffsize,buffer.begin());

    sg.set_frequency(880 * 2*3.141592653589793 / 8000);
    sg.reset_phase(0);
    // mix another sine into the buffer ...
    sg.generate(buffsize,buffer.begin(),plus&lt;double&gt;());

    for (int i=0; i&lt;buffsize; ++i) {
        cout &lt;&lt; buffer.at(i) &lt;&lt; '\n';
    }
}

</code></pre>
<p>- P</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pizer.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pizer.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pizer.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pizer.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pizer.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pizer.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pizer.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pizer.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pizer.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pizer.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pizer.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pizer.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pizer.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pizer.wordpress.com/512/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=512&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pizer.wordpress.com/2010/02/08/fast-digital-sine-oscillator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/16b8a9971551c67422fea478a6e73bae?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">pizer</media:title>
		</media:content>

		<media:content url="http://pizer.files.wordpress.com/2010/02/sine1.png" medium="image">
			<media:title type="html">circle</media:title>
		</media:content>

		<media:content url="http://pizer.files.wordpress.com/2010/02/sine21.png" medium="image">
			<media:title type="html">circle</media:title>
		</media:content>
	</item>
		<item>
		<title>C++0x: No concepts no fun?</title>
		<link>http://pizer.wordpress.com/2009/08/16/c0x-no-concepts-no-fun/</link>
		<comments>http://pizer.wordpress.com/2009/08/16/c0x-no-concepts-no-fun/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 16:29:43 +0000</pubDate>
		<dc:creator>pizer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[c++0x]]></category>
		<category><![CDATA[concepts]]></category>
		<category><![CDATA[cplusplus]]></category>
		<category><![CDATA[cplusplus0x]]></category>
		<category><![CDATA[generic programming]]></category>

		<guid isPermaLink="false">http://pizer.wordpress.com/?p=481</guid>
		<description><![CDATA[&#8220;Concepts&#8221; was a proposed C++0x feature that was supposed to make generic programming more enjoyable. The intention was to render many guru-level &#8220;template tricks&#8221; superfluous, produce much better compile-time error messages and provide &#8220;modular type checking&#8221; so errors can be caught early (even before a template is instantiated). Fact 1: Concepts have been removed from [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=481&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>&#8220;Concepts&#8221; was a proposed C++0x feature that was supposed to make generic programming more enjoyable. The intention was to render many guru-level &#8220;template tricks&#8221; superfluous, produce much better compile-time error messages and provide &#8220;modular type checking&#8221; so errors can be caught early (even before a template is instantiated).</p>
<ul>
<li> Fact 1: Concepts have been removed from the C++0x standardization effort. See Doug Gregor&#8217;s <a href="http://cpp-next.com/archive/2009/08/what-happened-in-frankfurt/">blog post</a>.
<li> Fact 2: C++0x still includes a whole lot of other core language and library features.
</ul>
<p><span id="more-481"></span></p>
<p>Fact 1 is as saddening as it is a relief, to be honest. Not all is lost, though. We don&#8217;t get modular type checking in C++0x. But we can restrict templates via <a href="http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error">SFINAE</a> which just got more powerful with the addition of <code>decltype</code> and the extension of SFINAE to expressions. What follows is a reminder of how this used to look in good ol&#8217; C++98. In this case it&#8217;s a simple pointer wrapper:</p>
<pre><code>  template&lt;typename T&gt;
  class ptr
  {
    T* p;
    template&lt;typename&gt; friend class ptr;
  public:
    ptr(T* p) : p(p) {}

    template&lt;typename U&gt;
    ptr(ptr&lt;U&gt; const&amp; x, typename enable_if&lt;
        is_convertible&lt;U*,T*&gt;::value
    &gt;::type* =0)
    : p(x.p) {}

    T&amp; operator*() const {return *p;}
    T* operator-&gt;() const {return p;}
    T* get() const {return p;}
  };

</code></pre>
<p>&#8220;What&#8217;s with the enable_if?&#8221; you may ask. This is &#8220;SFINAE&#8221; in action. If the compiler considers instantiating the constructor for some types T and U where U* is not convertible to T* we&#8217;d like the compiler to <em>ignore</em> this function. We can do this by using <code>enable_if</code> for selecting the type of an additional default parameter. <code>enable_if&lt;...&gt;::type</code> is only a valid type if the condition &#8212; the first parameter to enable_if &#8212; holds. Otherwise, the class won&#8217;t contain a typedef and when there is no such typedef the compiler has a hard time figuring out the constructor&#8217;s prototype. Since it deduces U and <code>enable_if&lt;...&gt;::type</code> depends on U such an error qualifies as &#8220;deduction failure&#8221; and makes the compiler just ignore the constructor. Great! This is exactly what we want! This way we can constrain templates and kick them out of the overload set <em>before</em> it tries to instantiate them <em>and even before</em> the compiler applies overload resolution.</p>
<p>Without this SFINAE trick the compiler will still complain when someone tries to convert a ptr&lt;int&gt; to a ptr&lt;double&gt;, for example. But it complains rather late after trying to instantiate the conversion constructor. Since int* is not convertible to double* it will point us to the initializer list &#8212; specifically <code>p(x.p)</code> &#8212; instead of pointing us directly to the offending line that requrested a <code>ptr&lt;int&gt;</code> to <code>ptr&lt;double&gt;</code> conversion. This is not as nice because it exposes implementation details and usually leads to many more lines of error messages. But exposition of implementation details is not the only problem. If we can&#8217;t eliminate functions from the overload set we might get ambiguity errors. For example, the set might contain two function template specializations that are deemed equally good in terms of overload resolution. But only one function can be instantiated, the other one will trigger a compilation error because some template parameters don&#8217;t satisfy all constraints. Since overload resolution is applied before instantiation the function call is ambiguous even though there&#8217;s only one candidate whose body is well-formed.</p>
<p>So, at first glance, it looks like SFINAE is just as good as a concept-&#8221;requires clause&#8221; when it comes to constraining a function template. Unfortunately, there are only a few things we can check for in C++98. Luckily, this changes with C++0x.</p>
<pre><code>  template&lt;typename T&gt; struct meta {
    static T&amp;&amp; src();
    static void sink(T);
  };

  template&lt;typename T&gt;
  class ptr
  {
    .....
    template&lt;typename U, typename =
      decltype( meta&lt;T*&gt;::sink( meta&lt;U*&gt;::src() ) )&gt;
    ptr(ptr&lt;U&gt; const&amp; x) : p(x.p) {}
    .....
  };

</code></pre>
<p>This example shows a constructor with a <em>default template argument</em> which would have been illegal in C++03. This allows us to get rid of spurious default parameters. The expression inside decltype() tries to pass a pointer of type U* to a function that accepts a pointer of type T*. The implementation of the type trait class is_convertible is based on the same idea. If U* is implicitly convertible to T* the declared type of this expression will be <code>void</code> which is the sink function&#8217;s return value type. If U* is not convertible to T* we have a &#8220;deduction failure&#8221; and the compiler will just ignore the function. I also could have just reused the is_convertible type trait like this</p>
<pre><code>  template&lt;typename U, typename = typename
    enable_if&lt;is_convertible&lt;U*,T*&gt;::value&gt;::type &gt;
  ptr(ptr&lt;U&gt; const&amp; x) : p(x.p) {}

</code></pre>
<p>This version is probably more readable. But the point I was trying to make with the former example was that it&#8217;s basically possible to check for the &#8220;well-formedness&#8221; of arbitrary expressions via <code>decltype</code>.</p>
<p>With a bit of macro trickery I was able to write a little header file that contains type trait classes for many many expressions that emulate the concepts HasPlus, HasDereference, HasAssign, Convertible, HasConstructor, etc. I call these &#8220;basic expression concepts&#8221;. Anything else can be built on top of these via composition. With a couple of other helper templates I managed to make composition fairly easy. This is what my &#8220;ForwardIterator concept&#8221; looks like, for example:</p>
<pre><code>  template&lt;typename Iter&gt; struct ForwardIterator
  : and_&lt;
    SemiRegular&lt;Iter&gt;, // copiable, assignable
    EqualityComparable&lt;Iter&gt;,
    HasDereference&lt;Iter&gt;,
    SameType&lt;typename HasPreincrement&lt;Iter&amp;&gt;::result_type,Iter&amp;&gt;
    Convertible&lt;typename HasPostincrement&lt;Iter&amp;&gt;::result_type,Iter&gt;
  &gt; {
    typedef typename HasDereference&lt;Iter&gt;::result_type reference;
  };

</code></pre>
<p>Every &#8220;basic expression concept&#8221; exposes a static const bool named value which is true if the expression is well-formed and false otherwise. In addition, it contains a typedef for <code>result_type</code> that either matches the expressions type or is equivalent to <code>invalid_type</code> (an empty struct). The use of invalid_type instead of a missing typedef prevents compilation errors and eases composition. So far, this looks like a nice framework for constraining templates. If there is interest, I could show you the whole 300 line header file. It also includes a nice macro that lets you write code like this:</p>
<pre><code>  template&lt;typename Iter, REQUIRES(ForwardIterator&lt;Iter&gt;)&gt;
  void foo(Iter a, Iter b) {
    // ...
  }

</code></pre>
<p>Conclusions: Extended SFINAE and decltype will make generic programming more enjoyable and possibibly more accessible to the masses. We can&#8217;t emulate modular type checking or concept-based overloading well, though. Constraints for class templates are even easier to check for with the help of <code>static_assert</code> and appropriate trait classes.</p>
<p>- P</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pizer.wordpress.com/481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pizer.wordpress.com/481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pizer.wordpress.com/481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pizer.wordpress.com/481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pizer.wordpress.com/481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pizer.wordpress.com/481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pizer.wordpress.com/481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pizer.wordpress.com/481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pizer.wordpress.com/481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pizer.wordpress.com/481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pizer.wordpress.com/481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pizer.wordpress.com/481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pizer.wordpress.com/481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pizer.wordpress.com/481/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=481&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pizer.wordpress.com/2009/08/16/c0x-no-concepts-no-fun/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/16b8a9971551c67422fea478a6e73bae?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">pizer</media:title>
		</media:content>
	</item>
		<item>
		<title>C++: Exploiting Copy Elisions</title>
		<link>http://pizer.wordpress.com/2009/06/25/c-exploiting-copy-elisions/</link>
		<comments>http://pizer.wordpress.com/2009/06/25/c-exploiting-copy-elisions/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 14:57:38 +0000</pubDate>
		<dc:creator>pizer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[copy elision]]></category>
		<category><![CDATA[cplusplus]]></category>
		<category><![CDATA[move semantics]]></category>
		<category><![CDATA[NRVO]]></category>
		<category><![CDATA[rvalues]]></category>
		<category><![CDATA[RVO]]></category>

		<guid isPermaLink="false">http://pizer.wordpress.com/?p=462</guid>
		<description><![CDATA[This article is about writing efficient code and giving the compiler opportunities for optimizations with respect to copying / &#8220;moving&#8221; objects in good old C++98. I use a simple function as an example but it should extend to other cases as well. The technique that is described here (&#8220;swaptimization&#8221;) is not entirely new and I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=462&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This article is about writing efficient code and giving the compiler opportunities for optimizations with respect to copying / &#8220;moving&#8221; objects in good old C++98. I use a simple function as an example but it should extend to other cases as well. The technique that is described here (&#8220;swaptimization&#8221;) is not entirely new and I can&#8217;t take credit for it. But it is probably little-known which is a good reason to write about it. <img src='http://s2.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><span id="more-462"></span></p>
<p>How would you write a function that accepts a string (with call-by-value semantics, source is not modified) and returns a reversed string by value? Perhaps something like this?</p>
<p><code>
<pre>  // version 1, C++98
  std::string flip_str(std::string const&amp; x)
  {
    std::string result = x;
    std::reverse(result.begin(),result.end());
    return x;
  }
</pre>
<p></code></p>
<p>You might have chosen to accept the parameter as reference-to-const to avoid unnecessary copying like I did above. But is this really the best thing we can do? To answer this, let&#8217;s check what and how a compiler is allowed to optimize (elide copies):</p>
<ul>
<li>If a function returns a function-local object (including temporary) it is allowed to elide the copy construction of the function&#8217;s result (<a href="http://en.wikipedia.org/wiki/Return_value_optimization">return value optimization</a>).</li>
<li>If an object <code>obj</code> of type <code>X</code> is initialized with a temporary of type <code>X</code>, the compiler is allowed to <a href="http://en.wikipedia.org/wiki/Return_value_optimization#Other_forms_of_copy_elision">elide this copy</a> and make <code>obj</code> an alias for this temporary. This includes the initialization of pass-by-value function parameters.</li>
</ul>
<p>Many C++ compiler actually perform these optimizations in many situations. So, in order to write efficient code we should try to exploit this.</p>
<p>If you check the implementation of <code>flip_str</code> version 1 again you&#8217;ll notice that I manually created a copy of the string, named <code>result</code>. In this case it&#8217;s easy for compilers to perform the named return value optimization. The return expression directly refers to the local object <code>result</code>. But we havn&#8217;t exploited temporaries as function arguments. What about this version?</p>
<p><code>
<pre>  // version 2, C++98
  std::string flip_str(std::string x)
  {
    std::reverse(x.begin(), x.end());
    return x;
  }
</pre>
<p></code></p>
<p>In version 2 we let the compiler worry about how <code>x</code> should come to life. In case the function is invoked with an lvalue argument the compiler will make sure that <code>x</code> refers to a copy. In case it&#8217;s a temporary the compiler might be able to elide the copy (<code>x</code> might alias the temporary object). So far so good. What about RVO? You could argue that <code>x</code> is a local variable. But to implement the mentioned copy elision optimizations the compiler probably generated a function that actually takes two addresses behind the scenes &#8212; one for the pass-by-value parameter <code>x</code> and one for the to-be-constructed return value. In version 2 the line &#8220;return x;&#8221; will then copy-construct the return value from x, so no NRVO is done. In C++0x we could simply write</p>
<p><code>
<pre>  // version 3, C++0x (relying on a fast move constructor)
  std::string flip_str(std::string x)
  {
    std::reverse(x.begin(), x.end());
    return std::move(x);
  }
</pre>
<p></code></p>
<p>Because <code>std::string</code> is a rather small object that only manages the character array through an internal pointer, move constructing strings can be expected to be much faster than copy constructions &#8212; unless your std::string implementation uses <a href="http://en.wikipedia.org/wiki/Copy-on-write">copy-on-write</a>. Still, it should be at least a bit faster. C++98 doesn&#8217;t support move construction directly. But there is a trick to get a similar behaviour in this case. Instead of a move construction we can use swap and rely on NRVO:</p>
<p><code>
<pre>  // version 4, C++98
  std::string flip_str(std::string x)
  {
    std::string result; // empty string, expected to be fast
    result.swap(x); // swapping, expected to be fast
    std::reverse(result.begin(), result.end());
    return result; // NRVO applicable
  }
</pre>
<p></code></p>
<p>Swapping is a kind of two-way moving. If your type supports fast swapping you should consider writing such a swap member function along with a free inline function and/or a specialization of the <code>std::swap</code> template so that algorithms can benefit from it.</p>
<p>Finally, let&#8217;s verify how many times the <code>string</code>&#8216;s copy constructor is invoked in the following example code:</p>
<p><code>
<pre>  // test code
  #include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  #include &lt;string&gt;

  using std::string;

  string source() {return "Hello World!";}

  // insert flip_str version 1, 2, or 4

  int main() {
    string hw = flip_str(flip_str(source()));
    std::cout &lt;&lt; hw &lt;&lt; &#39;\n&#39;;
  }
</pre>
<p></code></p>
<p>To verify how many times the copy c&#8217;tor of <code>string</code> is invoked we could replace the inclusion of the <code>&lt;string&gt;</code> header and the <code>using std::string;</code> declaration with the following dummy class:<br />
<code>
<pre>  // string replacement for testing
  struct string {
    string()
      {std::cout&lt;&lt;&quot;string::string()\n&quot;;}
    string(char const*)
      {std::cout&lt;&lt;&quot;string::string(char const*)\n&quot;;}
    string(string const&amp;)
      {std::cout&lt;&lt;&quot;string::string(string const&amp;)\n&quot;;}
    ~string()
      {std::cout&lt;&lt;&quot;string::~string()\n&quot;;}
    void swap(string &amp; with)
      {std::cout&lt;&lt;&quot;string::swap(string &amp;)\n&quot;;}
    char* begin() {return 0;}
    char* end() {return 0;}
  };
</pre>
<p></code></p>
<p>With this replacement and the g++ compiler I obtained the following results:<br />
<code>
<pre>
  Output for flip_str version 1 and 2:
  string::string(char const*)
  string::string(string const&amp;)
  string::string(string const&amp;)
  string::~string()
  string::~string()
  string::~string()
</pre>
<p></code><br />
The output for version 1 and 2 is exactly the same in this case. Here it is for version 4:<br />
<code>
<pre>
  Output for flip_str version 4:
  string::string(char const*)
  string::string()
  string::swap(string &amp;)
  string::string()
  string::swap(string &amp;)
  string::~string()
  string::~string()
  string::~string()
</pre>
<p></code><br />
The use of version 1 results in two copy constructions (total). The same is true for version 2. The use of version 4 didn&#8217;t involve <em>any</em> copy constructions. Just two default constructions and two swaps.</p>
<p>Conclusions:<br />
For types that have a fast default constructor and a fast associated swap function we can exploit the compilers&#8217; copy elision capabilities to increase performance. Other examples for these kinds of types include most STL containers (vector, deque, list, set, map, &#8230;).</p>
<p>- P</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pizer.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pizer.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pizer.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pizer.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pizer.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pizer.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pizer.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pizer.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pizer.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pizer.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pizer.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pizer.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pizer.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pizer.wordpress.com/462/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=462&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pizer.wordpress.com/2009/06/25/c-exploiting-copy-elisions/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/16b8a9971551c67422fea478a6e73bae?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">pizer</media:title>
		</media:content>
	</item>
		<item>
		<title>C++0x: Rvalue references 101</title>
		<link>http://pizer.wordpress.com/2009/05/27/c0x-rvalue-references-101/</link>
		<comments>http://pizer.wordpress.com/2009/05/27/c0x-rvalue-references-101/#comments</comments>
		<pubDate>Wed, 27 May 2009 12:22:02 +0000</pubDate>
		<dc:creator>pizer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[c++0x]]></category>
		<category><![CDATA[cplusplus]]></category>
		<category><![CDATA[cplusplus0x]]></category>
		<category><![CDATA[exception safety]]></category>
		<category><![CDATA[move semantics]]></category>
		<category><![CDATA[perfect forwarding]]></category>
		<category><![CDATA[rvalue reference]]></category>

		<guid isPermaLink="false">http://pizer.wordpress.com/?p=454</guid>
		<description><![CDATA[If found a very nice article about rvalue references and move semantics (primarily) by Dave Abrahams. It also considers return value optimizations, exception safety and in what state an object should be left in after its resources have been &#8220;stolen&#8221;. Here it is. [Edit 2009-08-18: The link doesn't seem to work anymore. However, Dave Abrahams [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=454&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If found a very nice article about rvalue references and move semantics (primarily) by Dave Abrahams. It also considers return value optimizations, exception safety and in what state an object should be left in after its resources have been &#8220;stolen&#8221;. <a href="https://www.boostpro.com/trac/wiki/BoostCon09/RValue101">Here</a> it is.</p>
<p><em>[Edit 2009-08-18:</em> The link doesn't seem to work anymore. However, Dave Abrahams has started to refine, extend, and convert it into a <a href="http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/">series of blog posts</a> on his new <a href="http://cpp-next.com/">site</a> <em>]</em></p>
<p>- P</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pizer.wordpress.com/454/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pizer.wordpress.com/454/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pizer.wordpress.com/454/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pizer.wordpress.com/454/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pizer.wordpress.com/454/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pizer.wordpress.com/454/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pizer.wordpress.com/454/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pizer.wordpress.com/454/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pizer.wordpress.com/454/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pizer.wordpress.com/454/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pizer.wordpress.com/454/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pizer.wordpress.com/454/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pizer.wordpress.com/454/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pizer.wordpress.com/454/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=454&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pizer.wordpress.com/2009/05/27/c0x-rvalue-references-101/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/16b8a9971551c67422fea478a6e73bae?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">pizer</media:title>
		</media:content>
	</item>
		<item>
		<title>C++0x: Do people understand rvalue references?</title>
		<link>http://pizer.wordpress.com/2009/04/13/c0x-do-people-understand-rvalue-references/</link>
		<comments>http://pizer.wordpress.com/2009/04/13/c0x-do-people-understand-rvalue-references/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 10:50:18 +0000</pubDate>
		<dc:creator>pizer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[c++0x]]></category>
		<category><![CDATA[cplusplus]]></category>
		<category><![CDATA[cplusplus0x]]></category>
		<category><![CDATA[references]]></category>
		<category><![CDATA[rvalue reference]]></category>

		<guid isPermaLink="false">http://pizer.wordpress.com/?p=386</guid>
		<description><![CDATA[[last update: 2009-05-27] Occasionally I happen to see a piece of C++ code from an eager author that tries to utilize rvalue references but doesn&#8217;t understand them and gets it wrong. I remember that I had trouble fully grasping the rvalue reference concept. But now, it seems like that was way in the past. I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=386&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="color:#808080;"><em>[last update: 2009-05-27]</em></span></p>
<p>Occasionally I happen to see a piece of C++ code from an eager author that tries to utilize rvalue references but doesn&#8217;t understand them and gets it wrong. I remember that I had trouble fully grasping the rvalue reference concept. But now, it seems like that was way in the past. I can hardly remember why it was so difficult to understand rvalue references.</p>
<p>It may have something to do with the sources people use to learn about this feature. For example I wouldn&#8217;t consider the C++0x wikipedia article to be a good introduction for rvalue references. It seems too short for that. But this wikipedia article <em>is</em> a nice overview of what kind of features can be expected.</p>
<p>Let me just mention the <em>real</em> sources worth reading:</p>
<ul>
<li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm">N1377</a>: Move Proposal (2002)</li>
<li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1385.htm">N1385</a>: Perfect Forwarding Problem (2002)</li>
<li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html">N2027</a>: A Brief Introduction to Rvalue References (2006)</li>
<li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2812.html">N2812</a>: A Safety Problem with Rvalue References (2008)</li>
<li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2844.html">N2844</a>: Fixing a Safety Problem with Rvalue References (Revision 1, 2009)</li>
<li><a href="https://www.boostpro.com/trac/wiki/BoostCon09/RValue101">Rvalue References 101</a>by Dave Abrahams</li>
</ul>
<p>I would recommend N2027 as an introduction and Dave Abraham&#8217;s article for more practical details including exception safety and return value optimizations.</p>
<p><span id="more-386"></span></p>
<p>How do you know whether you got something wrong? Well, if you feel the need to declare a function that returns a reference (be it lvalue reference or rvalue reference) to a function-local object, you got it wrong:</p>
<p><code>
<pre>  std::string&amp;&amp; wrong() {
     std::string r = "foo";
     r += "bar";
     return std::move(r);
  }

  std::string right() {
     std::string r = "foo";
     r += "bar";
     return r;
  }
</pre>
<p></code></p>
<p>This is an exampe adapted from a piece of code from another author who shall remain nameless. Part of the original author&#8217;s confusion is probably due to the name of the function <code>move</code> and the fact that it returns an rvalue reference. Truth is, the function <code>move</code> doesn&#8217;t move anything. It just converts an lvalue reference to an unnamed rvalue reference. Only when this unnamed rvalue reference is later used as source in constructing a new object it might actually be a move-construction. But it could also be a copy-construction in case the type doesn&#8217;t support moving. In this example you actually don&#8217;t need any C++0x specific syntax to benefit from movable string objects. Firstly, returning references to function-local objects is still a no-go. Rvalue references don&#8217;t make that any less true. Secondly, the <code>std::move()</code> call is unnecessary. In fact, it may even prevent a common optimization called <a href="http://www.efnetcpp.org/wiki/Return_value_optimization">NRVO</a>. Since this optimization is even better than a move-construction of the return value you should not disable NRVO by being &#8220;smart&#8221;. The new language rules already force compilers to move-construct return values from function-local objects in case they don&#8217;t support (N)RVO or simply can&#8217;t apply NRVO in a specific case.</p>
<p>I also saw an early example by Howard Hinnant (one of the authors of the rvalue reference proposal) in which he wrote a function that returned an rvalue reference. It didn&#8217;t refer to function-local objects but it referred to a reference parameter object which could have been a temporary and thus led to a dangling reference. He later said that he considers this example to be buggy and dangerous.</p>
<p><code>
<pre>
  string operator+(string const&amp; a, string const&amp; b) {
     string result;
     result.reserve( a.length() + b.length() );
     result += a;
     result += b;
     return result;
  }

  &#35;ifdef WRONG

  string&amp;&amp; operator+(string &amp;&amp; a, string const&amp; b) {
     a += b;
     return move(a);
  }

  &#35;else // correct version following ...

  string operator+(string &amp;&amp; a, string const&amp; b) {
     a += b;
     return move(a);
  }

  &#35;endif
</pre>
<p></code></p>
<p>The version named &#8220;wrong&#8221; in this example has an advantage and a disadvantage: It allows the recycling of temporary objects in an expression like <code>get_directory() + "/" + get_name() + ".png"</code> but it also allows the user to bind a reference (rvalue reference or just an ordinary reference to const) to the result which leads to a dangling reference. The 2nd version is much better in terms of safety because of a <a href="http://herbsutter.wordpress.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/">special rule</a> that makes the compiler extend the life-time of the returned temporary in some cases. Instead of recycling a temporary new temporaries will be move-constructed from old temporaries. This is believed to be a good trade-off.</p>
<p>In this situation we actually need <code>std::move</code> because a <em>named</em> rvalue reference behaves just like an lvalue. Anything that has a name or is an lvalue reference behaves like an lvalue. This includes a named rvalue reference.</p>
<p>Conclusions:</p>
<ul>
<li>Read a high-quality introduction to rvalue references before using this feature</li>
<li>Never ever return references to non-static function-local objects.</li>
<li>As a rule of thumb: Avoid returning rvalue references to reference parameters. The functions that do so are <code>std::move</code> and <code>std::forward</code>. They serve a special purpose.</li>
<li>Don&#8217;t use <code>std::move</code> on a non-static function-local object in a return expression. The compiler takes care of it already and may be even able to elide the move (NRVO).</li>
<li>Don&#8217;t use <code>std::move</code> on expressions that are known to return an rvalue (temporary object) already. If you do it may disable the RVO optimization.</li>
</ul>
<p>- P</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pizer.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pizer.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pizer.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pizer.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pizer.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pizer.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pizer.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pizer.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pizer.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pizer.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pizer.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pizer.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pizer.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pizer.wordpress.com/386/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=386&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pizer.wordpress.com/2009/04/13/c0x-do-people-understand-rvalue-references/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/16b8a9971551c67422fea478a6e73bae?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">pizer</media:title>
		</media:content>
	</item>
		<item>
		<title>A little C++0x finger exercise</title>
		<link>http://pizer.wordpress.com/2009/03/28/a-little-c0x-finger-exercise/</link>
		<comments>http://pizer.wordpress.com/2009/03/28/a-little-c0x-finger-exercise/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 19:59:41 +0000</pubDate>
		<dc:creator>pizer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[c++0x]]></category>
		<category><![CDATA[cplusplus]]></category>
		<category><![CDATA[cplusplus0x]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[rvalue reference]]></category>
		<category><![CDATA[variadic templates]]></category>

		<guid isPermaLink="false">http://pizer.wordpress.com/?p=362</guid>
		<description><![CDATA[Since the C++ compiler of GCC 4.3.2 comes with partial C++0x support and I had not tested all of these features yet, I decided to do a little finger exercise: &#8220;function&#8221;. &#8220;function&#8221; is a useful library feature of Boost, TR1, and the upcoming C++ standard library. It&#8217;s basically a class template mimicing the behaviour of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=362&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Since the C++ compiler of GCC 4.3.2 comes with partial <a href="http://en.wikipedia.org/wiki/C%2B%2B0x">C++0x</a> <a href="http://gcc.gnu.org/projects/cxx0x.html">support</a> and I had not tested all of these features yet, I decided to do a little finger exercise: &#8220;function&#8221;.</p>
<p>&#8220;function&#8221; is a useful library feature of Boost, TR1, and the upcoming C++ standard library. It&#8217;s basically a class template mimicing the behaviour of a function pointer. But it&#8217;s much more flexible because it applies to any &#8220;callable object&#8221; &#8212; function pointers and objects with overloaded function call operator. Also, the signatures don&#8217;t have to match perfectly as long as the parameters and return values are implicitly convertible. I&#8217;m going to show you how this library feature can be implemented with C++0x features &#8212; including variadic templates and rvalue references. But before going into any details let me give an example of how code that uses this &#8220;function&#8221; library might look like. In case you&#8217;re already familiar with this library feature you can skip the following piece of code and paragraph of text.</p>
<p><span id="more-362"></span></p>
<p><code>
<pre>  &#x23;include &lt;iostream&gt;
  &#x23;include &lt;ostream&gt;

  &#x23;include &quot;function.hpp&quot;

  using std::cout;
  using std::endl;

  int plus(int a, int b) {
     return a+b;
  }

  int minus(int a, int b) {
     return a-b;
  }

  struct accumulate_products {
     int* pacc;
     accumulate_products(int* p) : pacc(p) {}
     int operator()(int a, int b) const {
        *pacc += a*b;
        return *pacc;
     }
  };

  int main() {
     function&lt;int(int,int)&gt; f = plus;
     cout &lt;&lt; f(23,17) &lt;&lt; endl;
     f = minus;
     cout &lt;&lt; f(23,17) &lt;&lt; endl;
     int accum = 0;
     f = accumulate_products(&amp;accum);
     f(1,2);
     f(3,4);
     f(5,6);
     cout &lt;&lt; accum &lt;&lt; endl;
  }
</pre>
<p></code></p>
<p>As you might have noticed, the template parameter is actually a function signature. This example shows how we can assign function pointers to this object as well as some other function object.</p>
<p>As far as the C++ language is concerned this function signature is only one template parameter. Do we even need variadic templates for this? Yes, we do. We do because we want to support functions that take any number of parameters. This is what <a href="http://en.wikipedia.org/wiki/Variadic_function">&#8220;variadic&#8221;</a> is all about. What I&#8217;m going to do is to partially specialize the class template &#8220;function&#8221; with a template taking a variable number of type parameters. One for the return type and zero or more for the function&#8217;s parameter types. The rvalue reference feature is used to achieve both: move semantics and perfect forwarding. You may also notice some other tricks: &#8220;C++ type erasure&#8221; for runtime polymorphism, &#8220;copy &amp; swap&#8221; for efficient and exception-safe assignment. Without further ado, here&#8217;s the content of the file &#8220;function.hpp&#8221;:</p>
<p><code>
<pre>  &#x23;ifndef FUNCTION_HPP_INCLUDED
  &#x23;define FUNCTION_HPP_INCLUDED

  &#x23;include &lt;utility&gt;    // std::move, std::forward
  &#x23;include &lt;algorithm&gt;  // std::swap

  template&lt;typename R, typename ... P&gt;
  class abstract_callable
  {
  public:
     virtual ~abstract_callable() {}
     virtual R operator()(P ... p) = 0;
     virtual abstract_callable* clone() const = 0;

     template&lt;typename F&gt;
     // requires std::CopyConstructible&lt;F&gt;
     //       &amp;&amp; std::Callable&lt;F&amp;,P...&gt;
     //       &amp;&amp; std::Convertible&lt;std::Callable&lt;F&amp;,P...&gt;::result_type,R&gt;
     class wrapper /*[[check_names]]*/ : public abstract_callable
     {
     public:
        explicit wrapper(F const&amp; f) : func(f) {}
        explicit wrapper(F &amp;&amp; f) : func(std::move(f)) {}
        R operator() /*[[override]]*/ (P ... p) {
           return func(std::forward&lt;P&gt;(p) ...);
        }
        abstract_callable* clone /*[[override]]*/ () const {
           return new wrapper(*this);
        }
     private:
        F func;
     };
  };

  template&lt;typename Signature&gt;
  class function;

  template&lt;typename R, typename ... P&gt;
  class function&lt;R(P...)&gt;
  {
     typedef abstract_callable&lt;R,P...&gt; ac_type;
  public:
     typedef R result_type;

     function() : ptr(0) {}

     template&lt;typename F&gt;
     // requires std::CopyConstructible&lt;F&gt;
     //       &amp;&amp; std::Callable&lt;F&amp;,P...&gt;
     //       &amp;&amp; std::Convertible&lt;std::Callable&lt;F&amp;,P...&gt;::result_type,R&gt;
     function(F f) : ptr(0) {
        typedef typename ac_type::template wrapper&lt;F&gt; wrapper_t;
        ptr = new wrapper_t(std::move(f));
     }

     function(function const&amp; f) : ptr(f.ptr-&gt;clone()) {}
     function(function &amp;&amp; f) : ptr(f.ptr) {f.ptr=0;}
     ~function() { delete ptr; }

     void swap(function &amp; f) {
       using std::swap;
       swap(ptr,f.ptr);
     }

     function&amp; operator=(function f) {
       swap(f);
       return *this;
     }

     R operator()(P ... p) const {
       return (*ptr)(std::forward&lt;P&gt;(p) ...);
     }
  private:
     ac_type* ptr;
  };

  template&lt;typename Signature&gt;
  inline void swap(function&lt;Signature&gt; &amp; f1, function&lt;Signature&gt; &amp; f2) {
     f1.swap(f2);
  }

  &#x23;endif // FUNCTION_HPP_INCLUDED
</pre>
<p></code></p>
<p>There you go! This compiles with the option -std=c++0x and seems to work (I didn&#8217;t test it that much).</p>
<p>Try implementing it with C++03 (current C++) and you&#8217;ll end up writing many more lines to support up to N function parameters. For every number of parameters you need to write a specialization. Sure, with a bit of macro trickery you can reduce the typing effort but the number of lines of code the preprocessor will emit is still very high. Also, without perfect forwarding you have to think about how parameters are accepted and forwarded to the next function. There&#8217;s no perfect solution for that in C++03.</p>
<p>Besides variadic templates and rvalue references I hinted at two other features. Have you spot it? I commented them out with because GCC does not yet support concepts and attributes. The idea behind these kinds of attributes is to make the compiler check the member functions&#8217; names and reject the code if something doesn&#8217;t add up. But this has not yet been voted into the draft. It&#8217;s just a <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2852.html">proposal</a> for now. The concepts part makes the compiler type-check the templates some more and generate error messages that are hopefully human-readable in case the user tries to assign incompatible function pointers/objects.</p>
<p>Finally, I&#8217;d like to mention that this implementation is just a simple demonstration. There&#8217;s some room for improvement. I think it&#8217;s reasonable to expect that a good standard library implementation will outperform this simple version for &#8220;small&#8221; function objects such as raw function pointers and <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2845.html">reference closures</a>.</p>
<p>- P</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pizer.wordpress.com/362/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pizer.wordpress.com/362/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pizer.wordpress.com/362/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pizer.wordpress.com/362/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pizer.wordpress.com/362/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pizer.wordpress.com/362/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pizer.wordpress.com/362/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pizer.wordpress.com/362/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pizer.wordpress.com/362/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pizer.wordpress.com/362/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pizer.wordpress.com/362/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pizer.wordpress.com/362/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pizer.wordpress.com/362/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pizer.wordpress.com/362/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=362&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pizer.wordpress.com/2009/03/28/a-little-c0x-finger-exercise/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/16b8a9971551c67422fea478a6e73bae?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">pizer</media:title>
		</media:content>
	</item>
		<item>
		<title>Surprizing C++ reference-binding rules</title>
		<link>http://pizer.wordpress.com/2009/03/25/surprizing-c-reference-binding-rules/</link>
		<comments>http://pizer.wordpress.com/2009/03/25/surprizing-c-reference-binding-rules/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 11:56:08 +0000</pubDate>
		<dc:creator>pizer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[c++0x]]></category>
		<category><![CDATA[cplusplus]]></category>
		<category><![CDATA[cplusplus0x]]></category>
		<category><![CDATA[noncopyable]]></category>
		<category><![CDATA[references]]></category>
		<category><![CDATA[rvalues]]></category>

		<guid isPermaLink="false">http://pizer.wordpress.com/?p=354</guid>
		<description><![CDATA[Isn&#8217;t it amazing? You see a very simple piece of code that looks perfectly fine and wonder why it is supposed to be ill-formed: struct foo { foo(); private: foo(const foo&#38; rhs); }; void func1(const foo&#38;); void func2() { foo o; func1( o ); // ok func1( foo() ); // ill-formed, foo's copy-ctor is private [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=354&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Isn&#8217;t it amazing? You see a very simple piece of code that looks perfectly fine and wonder why it is supposed to be ill-formed:</p>
<pre><code>  struct foo {
    foo();
  private:
    foo(const foo&amp; rhs);
  };

  void func1(const foo&amp;);

  void func2() {
    foo o;
    func1( o ); // ok
    func1( foo() );  // ill-formed, foo's copy-ctor is private
  }

</code></pre>
<p>Although an rvalue is allowed to bind to a reference-to-const, the C++ standard allows a compiler to copy an rvalue to another temporary rvalue before binding it to a reference-to-const. Obviously, you need to have a public copy constructor for that.</p>
<p>I really fail to see the merit in this rule. It seems that <em>every</em> available C++ compiler <em>does not</em> create an extra temporary object here and some of these compilers don&#8217;t even bother to report an error in this situation (g++ version 3.4.5 accepts the above code).</p>
<p>Fortunately, the upcoming version of the C++ standard removes the CopyConstructible requirement for reference binding. It makes the code above well-formed.</p>
<p>Source: <a href="http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/a8f816cf238e831c#">this usenet thread</a></p>
<p>- P</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pizer.wordpress.com/354/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pizer.wordpress.com/354/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pizer.wordpress.com/354/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pizer.wordpress.com/354/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pizer.wordpress.com/354/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pizer.wordpress.com/354/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pizer.wordpress.com/354/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pizer.wordpress.com/354/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pizer.wordpress.com/354/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pizer.wordpress.com/354/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pizer.wordpress.com/354/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pizer.wordpress.com/354/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pizer.wordpress.com/354/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pizer.wordpress.com/354/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=354&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pizer.wordpress.com/2009/03/25/surprizing-c-reference-binding-rules/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/16b8a9971551c67422fea478a6e73bae?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">pizer</media:title>
		</media:content>
	</item>
		<item>
		<title>Books, Books, Books (on C++)</title>
		<link>http://pizer.wordpress.com/2009/02/22/books-books-books-on-c/</link>
		<comments>http://pizer.wordpress.com/2009/02/22/books-books-books-on-c/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 00:01:33 +0000</pubDate>
		<dc:creator>pizer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cplusplus]]></category>

		<guid isPermaLink="false">http://pizer.wordpress.com/?p=350</guid>
		<description><![CDATA[I already said it once in another post but I think it deserves to be mentioned again: Don&#8217;t underestimate the usefulness of good books about C++! Prefer buying a good book over searching the internet for tutorials, examples, explanations if you want to learn the language and/or learn about useful idioms, guidelines, etc&#8230; I recently [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=350&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I already said it once in another post but I think it deserves to be mentioned again:  Don&#8217;t underestimate the usefulness of <em>good</em> books about C++!  Prefer buying a <em>good</em> book over searching the internet for tutorials, examples, explanations if you want to learn the language and/or learn about useful idioms, guidelines, etc&#8230;</p>
<p>I recently added two other books to my personal library: &#8220;Effective C++&#8221; and &#8220;C++ Coding Standards&#8221;.  It took me only 3 weeks to finish both books.  That means they were interesting and insightful. <img src='http://s2.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Now, it&#8217;s these four C++ books that are in my posession:</p>
<ul>
<li>The C++ Programming Language (Special Edition) by Bjarne Stroustrup</li>
<li>Modern C++ Design by Andrei Alexandrescu</li>
<li>Effective C++ (Third Edition) by Scott Meyers</li>
<li>C++ Coding Standards by Herb Sutter &amp; Andrei Alexandrescu</li>
</ul>
<p>I wouldn&#8217;t mention them here if I didn&#8217;t like them. <img src='http://s2.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>- P</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pizer.wordpress.com/350/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pizer.wordpress.com/350/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pizer.wordpress.com/350/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pizer.wordpress.com/350/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pizer.wordpress.com/350/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pizer.wordpress.com/350/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pizer.wordpress.com/350/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pizer.wordpress.com/350/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pizer.wordpress.com/350/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pizer.wordpress.com/350/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pizer.wordpress.com/350/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pizer.wordpress.com/350/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pizer.wordpress.com/350/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pizer.wordpress.com/350/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=350&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pizer.wordpress.com/2009/02/22/books-books-books-on-c/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/16b8a9971551c67422fea478a6e73bae?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">pizer</media:title>
		</media:content>
	</item>
		<item>
		<title>C++0x: concepts &amp; rvalue references &#8212; 2nd round</title>
		<link>http://pizer.wordpress.com/2009/01/08/c0x-concepts-rvalue-references-2nd-round/</link>
		<comments>http://pizer.wordpress.com/2009/01/08/c0x-concepts-rvalue-references-2nd-round/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 17:04:07 +0000</pubDate>
		<dc:creator>pizer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[c++0x]]></category>
		<category><![CDATA[concepts]]></category>
		<category><![CDATA[cplusplus]]></category>
		<category><![CDATA[cplusplus0x]]></category>
		<category><![CDATA[rvalue reference]]></category>

		<guid isPermaLink="false">http://pizer.wordpress.com/?p=302</guid>
		<description><![CDATA[Last update: 2009-02-1 Concepts and rvalue references are two new features that are going to be included in the next official version of the C++ language with high probability. I&#8217;m not going to mention the basics behind those two features because it has been done before in other articles. Try this google video or this [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=302&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="color:#808080;"><em>Last update: 2009-02-1</em></span></p>
<p>Concepts and rvalue references are two new features that are going to be included in the next official version of the <a href="http://en.wikipedia.org/wiki/C%2B%2B0x">C++ language</a> with high probability. I&#8217;m not going to mention the basics behind those two features because it has been done before in other articles. Try <a href="http://video.google.com/videoplay?docid=-1790714981047186825">this google video</a> or <a href="http://www.devx.com/SpecialReports/Article/38864">this article</a> for an introduction to concepts by Doug Gregor and <a href="http://www.artima.com/cppsource/rvalue.html">this article</a> for an introduction to rvalue references by Howard Hinnant et al.</p>
<p>I already wrote a post on concepts and rvalue references <a href="2008/12/06/c0x-interaction-between-rvalue-references-concepts/">here</a> which discussed some problems when concepts are combined with rvalue references. It turned out that David Abrahams and Doug Gregor were also aware of the first issue I addressed. They <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2812.html">published</a> an article one day before I wrote about it. In this post I&#8217;m going to address other arguments (consistency and intuitivity) for why I think their proposed resolution is a good idea and what other language modification can increase consistency. Before I go into the last bit I will shed some light on what function requirements actually mean and how they can be satisfied.</p>
<p><strong><em>Update 2009-02-1:</em></strong>[ It turns out you can't exprapolate the meaning of function requirements from the concept_map checking rules because they don't cover all use cases in constrained contexts (see <a href="http://groups.google.de/group/comp.std.c++/browse_thread/thread/63ddc68630bf074f#">hole in concepts system</a>). So, keep this in mind while reading the rest of this article.]</p>
<p><span id="more-302"></span></p>
<p>Let&#8217;s recap: Under <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2800.pdf">current rules</a> an rvalue reference can bind to both rvalues <em>and</em> lvalues. This is likely to cause trouble in cases where we rely on overload resolution between <code>&amp;</code> and <code>&amp;&amp;</code> to pick the correct function when concept requirements or SFINAE is involved and a <code>const&amp;</code> overload is removed silently. I&#8217;m in total agreement with the article by Abrahams and Gregor and their proposed resolution. Also, the current rules w.r.t. the meaning of reference types and reference qualifiers is slightly inconsistent, especially among different contexts including concepts. The proposal by Abrahams et al improves the situation in terms of consistency, too.</p>
<p>Reference types: <code>&amp;</code> may bind to lvalues only. <code>const&amp;</code> may also bind to rvalues. <code>&amp;&amp;</code> is allowed to bind to <em>both</em>: rvalues <em>and</em> lvalues. The current <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2800.pdf">proposal N2800</a> also includes an extension of the rvalue reference proposal to <code>*this</code> for member functions. However, it is not 100% consistent with respect to the meaning of <code>&amp;&amp;</code> as used in the basic proposal. The ref qualifer <code>&amp;</code> added after a member function declaration prevents the function from being callable on rvalues. The ref qualifier <code>&amp;&amp;</code> prevents the member function from being callable on lvalues. In isolation this seems like a very reasonable way to go for member functions and ref qualifiers since the lack of a ref qualifier makes the function callable in both cases. But the meaning of <code>&amp;&amp;</code> is obviously slightly different. The proposed resolution by Abrahams and Gregor &#8212; making <code>&amp;&amp;</code> <em>generally</em> bind only to rvalues &#8212; would unify the meaning of <code>&amp;&amp;</code> here. This is a good thing &#8212; after all, they are called rvalue references. <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Reference types in concepts: The meaning of reference types and ref qualifiers for function requirements in concepts is special. A function requirement is not the same as a function prototype for which a function definition must exist that uses exactly the same signature. A function requirement is more easily satisfied. Think of function requirements as &#8220;invocation signatures&#8221;. They define what kind of invocation expressions are guaranteed to be well-formed. Here is an example:</p>
<pre><code>  concept HasFoo&lt;typename T&gt; {
    void foo(T);
  }

  long foo(long);

  concept_map HasFoo&lt;int&gt; {} // OK

</code></pre>
<p>You see that it&#8217;s OK to have a function with a slightly different signature. The important thing is that the requirement only guarantees a specific invocation expression to be well-formed. An <code>int</code> can be implicitly converted to a <code>long</code> and the function&#8217;s result is always &#8220;convertible&#8221; to <code>void</code>. Implicit conversion is not the only thing that might differ. The function foo could also have accepted a <code>const&amp;</code> parameter as long as the invocation expression implied by the concept&#8217;s function requirement is well-formed. In this case the expression is <code>foo(p)</code> where <code>p</code> is of type T. For T=int this expression is obviously well-formed because of <code>long foo(long)</code>. Question: What do you think is the meaning of the following function requirement:</p>
<pre><code>  concept HasFooRvalRef&lt;typename T&gt; {
    void foo(T&amp;&amp;);
  }

</code></pre>
<p>Under current rules a function that is <em>declared</em> like this accepts both: rvalues and lvalues. You know that by now. But in the context of concepts this is a function <em>requirement</em>. It is satisfied &#8212; according to section 14.9.2.1 &#8212; if the expression <code>foo(x)</code> is well-formed where <code>x</code> is an rvalue of type <code>T</code>. So, the only thing this function requirement guarantees is that the function <code>foo</code> can be invoked with an rvalue argument of type <code>T</code>. Note: There is no word on lvalues. Invoking <code>foo</code> with an lvalue argument of type <code>T</code> is not part of this concept and thus can&#8217;t be used in restricted templates unless there is also some other requirement specifically allowing this. Again, the proposed resolution by Abrahams and Gregor would unify the meaning of <code>&amp;&amp;</code> here.</p>
<p>Finally let&#8217;s look at how member function requirements can be satisfied. Remember that a member function that is declared without a ref qualifier can be invoked on rvalues and lvalues. What do you think the following concept means?</p>
<pre><code>  concept HasMemberFoo&lt;typename T&gt; {
    void T::foo();
  }

  template&lt;HasMemberFoo T&gt;
    requires DefaultConstructible&lt;T&gt;
  void test(T&amp; lvalue_ref) {
    lvalue_ref.foo(); // OK?
    T().foo(); // OK?
  }

</code></pre>
<p>The question is: Will this compile? To answer this we need to check again section 14.9.2.1 of <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2800.pdf">N2800</a>. It says that the expression &#8216;E&#8217; that needs to be well-formed in order to satisfy this member function requirement is <code>x.foo()</code> where <code>x</code> is an <em>lvalue</em> of type T. So, the concept doesn&#8217;t say anything about the function <code>foo</code> being callable on rvalues despite its requirement&#8217;s appearence. Actually, there is no difference between this concept and the following one:</p>
<pre><code>  concept HasMemberFooRef&lt;typename T&gt; {
    void T::foo() &amp;; // &lt;-- added ref qualifier
  }

</code></pre>
<p>This is because the implied expression &#8216;E&#8217; that needs to be well-formed is exatly the same. Only in the case of <code>&amp;&amp;</code> as a ref qualifier instead the <code>x</code> in the expression is treated as an rvalue of type T &#8212; which in turn doesn&#8217;t say anything about lvalues. But this is actually no problem because it is what we expect from the <code>&amp;&amp;</code> qualifier for member functions. So, the latter case is consistent.</p>
<p>In case you&#8217;re still with me you will probably agree with me that the satisfaction rule for a member function requirement that lacks a ref qualifier is counter-intuitive. Such a requirement looks like a promise for being able to call the function on rvalues as well as lvalues just like it is the case in good old C++98. This is why I&#8217;d like to propose changing this satisfaction rule a bit. In terms of consistency and intuitivity it seems best to require that such a member function requirement is satisfied only when it can be invoked on lvalues <em>and</em> rvalues. Instead of requiring only one expression &#8220;E&#8221; to be well-formed we would require two expressions &#8220;E1&#8243; and &#8220;E2&#8243; to be well-formed that only differ in <code>x</code> being an lvalue in E1 and an rvalue in E2. This change of the rule would <em>not</em> render the C++ language less expressive as far as I can tell. But it would certainly increase consistency with respect to the behaviour of ref qualifiers (or lack thereof) for member functions when both contexts (declaration versus requirement) are compared.</p>
<p>If you want to comment on my proposal to change the rules in 14.9.2.1 please consider writing a response for <a href="http://groups.google.de/group/comp.std.c++/browse_thread/thread/6f3b093d7083ac18#">this usenet thread</a>.</p>
<p>- P<em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pizer.wordpress.com/302/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pizer.wordpress.com/302/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pizer.wordpress.com/302/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pizer.wordpress.com/302/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pizer.wordpress.com/302/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pizer.wordpress.com/302/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pizer.wordpress.com/302/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pizer.wordpress.com/302/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pizer.wordpress.com/302/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pizer.wordpress.com/302/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pizer.wordpress.com/302/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pizer.wordpress.com/302/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pizer.wordpress.com/302/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pizer.wordpress.com/302/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=302&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pizer.wordpress.com/2009/01/08/c0x-concepts-rvalue-references-2nd-round/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/16b8a9971551c67422fea478a6e73bae?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">pizer</media:title>
		</media:content>
	</item>
		<item>
		<title>Writing portable C and C++ code</title>
		<link>http://pizer.wordpress.com/2008/12/18/writing-portable-c-and-c-code/</link>
		<comments>http://pizer.wordpress.com/2008/12/18/writing-portable-c-and-c-code/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 15:11:13 +0000</pubDate>
		<dc:creator>pizer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cplusplus]]></category>
		<category><![CDATA[portable code]]></category>

		<guid isPermaLink="false">http://pizer.wordpress.com/?p=272</guid>
		<description><![CDATA[Width and value ranges of Integer types C and C++ only specify minimum requirements regarding the width and value range of different integer types and how these are converted to each other. If you want to write portable code you should be aware of this. Check out the article Integer Types in C and C++. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=272&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>Width and value ranges of Integer types</h3>
<p>C and C++ only specify minimum requirements regarding the width and value range of different integer types and how these are converted to each other. If you want to write portable code you should be aware of this. Check out the article <a href="http://home.att.net/~jackklein/c/inttypes.html">Integer Types in C and C++</a>.</p>
<p><span id="more-272"></span></p>
<p>The header <code>&lt;stdint.h&gt;</code> is also very useful. Unfortunately it&#8217;s a C99 header. You may be able to use it in C++ code, too. Some vendors (recent GNU C++ compiler) even allow you to include <code>&lt;cstdint&gt;</code> which will be officially present in the upcoming C++ standard. This header includes various typedefs for integer types like <code>int_fast16_t</code>.</p>
<h3>Integer conversion</h3>
<p>Signed to unsigned conversion:</p>
<pre><code>  int a = -5;
  unsigned b = ~ unsigned(a);
  cout &lt;&lt; b &lt;&lt; endl;

</code></pre>
<p>This will output 4 because the result of a signed to unsigned conversion is guaranteed to be congruent to the original <em>value</em> modulo 2^N where N is the number of bits used for the unsigned variable. I emphasize the word <em>value</em> because this does not imply that the unsigned variable will have the same representation in bits. This is only true for signed numbers stored in <a href="http://en.wikipedia.org/wiki/Two's_complement">two&#8217;s complement</a> which is <em>not</em> mandated by the standards.</p>
<p>Unsigned to signed conversion isn&#8217;t as well-defined as the other direction. The conversion is only defined for those values that can be represented as signed number. Values that are higher than that lead to implementation-defined behaviour.</p>
<pre><code>  unsigned a = INT_MAX + 3u;
  int b = a;
  cout &lt;&lt; b &lt;&lt; endl;

</code></pre>
<p>Your C/C++ implementation is basically allowed to do anything. Your compiler&#8217;s manual should document this as one of the implementation-defined rules. It&#8217;s very likely that in case your platform uses <a href="http://en.wikipedia.org/wiki/Two's_complement">two&#8217;s complement</a> for representing negative numbers your compiler will simply reinterpret the bit pattern as signed number which also implicitly obeys the &#8220;congruent modulo 2^N&#8221;-rule. Still, you might want to include some unit tests to verify this if you rely on this behaviour.</p>
<h3>Making your code 64bit compatible</h3>
<p>An <code>int</code> is still <em>usually</em> 32 bit. On modern platforms pointers are likely to be 64 bit long. Be aware of that. Make use of <code>std::size_t</code> and <code>std::ptrdiff_t</code> when it makes sense. <code>std::size_t</code> should be big enough to count the amount of addressable bytes in your RAM. <code>std::ptrdiff_t</code> is the result of a pointer difference and might be able to represent more values than <code>int</code>.</p>
<h3>Handling binary data portably</h3>
<p>Sometimes you may want to access raw data via a pointer to unsigned char &#8212; for example to decode contents of a binary file that contains fields of various formats (ie. signed 16 bit int in two&#8217;s complement and little endian byte order). The standards allow this but only for char and unsigned char. Casting pointers to other pointer types might violate alignment and <a href="http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.html">aliasing</a> rules. You&#8217;d be leaving the territory of well-defined behaviour. Even the common union trick for converting data is not supported by the official C and C++ standards. But accessing raw data as a sequence of characters (both plain and unsigned char) is fine.</p>
<p>A reasonable implementation to read 16 bit integers in little endian byte order might look like this:</p>
<pre><code>  #include &lt;climits&gt;
  #include &lt;stdint.h&gt;
  #if CHAR_BIT != 8
  #error Only supported for 8bit chars
  #endif

  template&lt;bool B, typename R&gt; struct enable_if {};
  template&lt;typename R&gt; struct enable_if&lt;true,R&gt; { typedef R type; };

  inline uint16_t get_u16le(const void* pv) {
    const unsigned char* pc = static_cast&lt;const unsigned char*&gt;(pv);
    return pc[0] | (static_cast&lt;uint16_t&gt;(pc[1]) &lt;&lt; 8);
  }

  inline int16_t get_s16le(const void* pv) {
    enable_if&lt;(signed(~1u) == -2), int16_t&gt;::type tmp = get_u16le(pv);
    // Relying on two's complement and reinterpretation of bit pattern
    return tmp;
  }

</code></pre>
<p>The last bit is probably overkill and might not catch all weird platforms that don&#8217;t support this trick. But I feel the need to express that I&#8217;m making use of a common but still implementation-defined behaviour.</p>
<p>Cheers!<br />
- P</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pizer.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pizer.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pizer.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pizer.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pizer.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pizer.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pizer.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pizer.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pizer.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pizer.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pizer.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pizer.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pizer.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pizer.wordpress.com/272/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pizer.wordpress.com&amp;blog=5005041&amp;post=272&amp;subd=pizer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pizer.wordpress.com/2008/12/18/writing-portable-c-and-c-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/16b8a9971551c67422fea478a6e73bae?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">pizer</media:title>
		</media:content>
	</item>
	</channel>
</rss>
