<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Well, technically... &#187; fun</title>
	<atom:link href="http://blog.hashpling.org/category/fun/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hashpling.org</link>
	<description>Don&#039;t worry, I&#039;ve hidden all the complexity behind a macro.</description>
	<lastBuildDate>Sun, 03 Jan 2010 08:53:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Exponent operator overloading</title>
		<link>http://blog.hashpling.org/exponent-operator-overloading/</link>
		<comments>http://blog.hashpling.org/exponent-operator-overloading/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 00:55:50 +0000</pubDate>
		<dc:creator>hashpling</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[fun]]></category>

		<guid isPermaLink="false">http://blog.hashpling.org/?p=92</guid>
		<description><![CDATA[In C++ you can&#8217;t create new operators to overload, you can only overload the operators that are already defined in the language. Often, an exponentiation operator is cited as an example of a new operator that you might want to have, so something like this isn&#8217;t possible. #include &#60;cmath&#62; #include &#60;cassert&#62; int main() { Double [...]]]></description>
			<content:encoded><![CDATA[<p>In C++ you can&#8217;t create new operators to overload, you can only overload the operators that are already defined in the language. Often, an exponentiation operator is cited as an example of a new operator that you might want to have, so something like this isn&#8217;t possible.</p>
<pre class="brush: cpp; gutter: false;">

#include &lt;cmath&gt;
#include &lt;cassert&gt;

int main()
{

    Double d1 = 81.0;
    Double d2 = 0.5;
    Double d3 = 4.0;
    Double d4 = 3.0;

    assert( d1 * d2 == 40.5 );
    assert( d1 ** d2 == 9.0 );
    assert( d4 ** d3 == d1 );
    assert( d4 * d3 == 12.0 );

    return 0;
}
</pre>
<p>(Excuse the use of <code>==</code> with floating point, in this toy example all the numbers have exact representations.)</p>
<p>Or is it?</p>
<p>Actually, it is. By suitable abuse of the prefix version of <code>*</code> and a simple proxy class we can get the effect of a <code>**</code> operator although you might not always get the precedence that you expect.</p>
<pre class="brush: cpp; gutter: false;">
#include &lt;cmath&gt;

template&lt;class T&gt;
struct Exponent
{
    explicit Exponent(T t)
        : val(t)
    {
    }

    T val;
};

template&lt;class T&gt;
class Number
{
public:

    Number(T t)
        : val(t)
    {
    }

    Exponent&lt;T&gt; operator*() const
    {
        return Exponent&lt;T&gt;(val);
    }

    Number operator*(const Exponent&lt;T&gt;&amp; e) const
    {
        return Number(std::pow(val, e.val));
    }

    operator T() const
    {
        return val;
    }

private:

    T val;
};

typedef Number&lt;double&gt; Double;
</pre>
<p>Neat, if evil.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hashpling.org/exponent-operator-overloading/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
