<?xml version="1.0" encoding="UTF-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://everything2.com/">
    <title>genneth's New Writeups</title>
    <link rel="alternate" type="text/html" href="http://everything2.com/index.pl?node=Everything%20User%20Search&amp;usersearch=genneth" />
    <link rel="self" type="application/atom+xml" href="?node=New%20Writeups%20Atom%20Feed&amp;type=ticker&amp;foruser=genneth" />
    <id>http://everything2.com/?node=New%20Writeups%20Atom%20Feed&amp;foruser=genneth</id>
    <updated>2005-03-11T09:07:20Z</updated>
<entry><title>conditional operator (idea)</title><link rel="alternate" type="text/html" href="http://everything2.com/user/genneth/writeups/conditional+operator"/><id>http://everything2.com/user/genneth/writeups/conditional+operator</id><author><name>genneth</name><uri>http://everything2.com/user/genneth</uri></author><published>2005-03-11T09:07:20Z</published><updated>2005-03-11T09:07:20Z</updated>
<content type="html">&lt;p&gt;
There is something which has been ignored by all the &lt;a href=&quot;/title/WU&quot;&gt;WU&lt;/a&gt;'s so far -- the &lt;a href=&quot;/title/type&quot;&gt;type&lt;/a&gt; of the expression. In &lt;a href=&quot;/title/C%252B%252B&quot;&gt;C++&lt;/a&gt; all expressions are &lt;a href=&quot;/title/statically+typed&quot;&gt;statically typed&lt;/a&gt;, so the &lt;a href=&quot;/title/compiler&quot;&gt;compiler&lt;/a&gt; must know what all the types of the expressions are. For the &lt;em&gt;simple&lt;/em&gt; &amp;lt;grin&amp;gt; case, the following rules are obeyed for &lt;code&gt;( T ? A : B )&lt;/code&gt;:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If A and B are of the same type, then the entire expression has that type.&lt;/li&gt;
&lt;li&gt;If A can be converted to B, it does so.&lt;/li&gt;
&lt;li&gt;Then the other way is considered.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
This is the simple case because we have ignored l/r-values, and &lt;a href=&quot;/title/const&quot;&gt;const&lt;/a&gt;/&lt;a href=&quot;/title/volatile&quot;&gt;volatile&lt;/a&gt; cases. It happens that there are some more conditions for those cases. &lt;a href=&quot;/title/Language+lawyers&quot;&gt;Language lawyers&lt;/a&gt; may refer to &lt;a href=&quot;/title/C%252B%252B+Standard&quot;&gt;The Good Book&lt;/a&gt; for more details.
&lt;/p&gt;
&lt;p&gt;
Why is this important? Because it allows us to get the type of an expression without having to evaluate it. Suppose we have the following functions:
&lt;/p&gt;
&lt;pre&gt;
template &amp;lt;typename T&amp;gt;
&lt;a href=&quot;/title/std%253A%253Astring&quot;&gt;std::string&lt;/a&gt; get_type_name(T x) {return &quot;unknown&quot;;}&lt;/pre&gt;&amp;hellip;</content>
</entry><entry><title>concept (idea)</title><link rel="alternate" type="text/html" href="http://everything2.com/user/genneth/writeups/concept"/><id>http://everything2.com/user/genneth/writeups/concept</id><author><name>genneth</name><uri>http://everything2.com/user/genneth</uri></author><published>2005-02-22T18:25:51Z</published><updated>2005-02-22T18:25:51Z</updated>
<content type="html">&lt;p&gt;
In &lt;a href=&quot;/title/C%252B%252B&quot;&gt;C++&lt;/a&gt;, Concepts were first (?) introduced by &lt;a href=&quot;/title/STL&quot;&gt;STL&lt;/a&gt;. Unlike most of C++, they are not &lt;a href=&quot;/title/Type+checking&quot;&gt;compiler enforced&lt;/a&gt;. Rather, they are documents that specify the structure needed by objects to work with &lt;a href=&quot;/title/template&quot;&gt;templated&lt;/a&gt; algorithms/frameworks. Concepts may be fulfilled by Models, which are just types (including builtin types such as &lt;a href=&quot;/title/int&quot;&gt;int&lt;/a&gt;). For example, the STL specifies the Concept of Sequence, which is a refinement (think &lt;a href=&quot;/title/subclass&quot;&gt;subclass&lt;/a&gt;) of Container. Sequence may be modelled by &lt;a href=&quot;/title/vector&quot;&gt;vector&lt;/a&gt;, &lt;a href=&quot;/title/list&quot;&gt;list&lt;/a&gt;, &lt;a href=&quot;/title/tree&quot;&gt;tree&lt;/a&gt;, etc. As long as a type fulfills certain requirements, such as having begin and end methods that behave in a certain way, it may be considered to Model a Concept. There is no explicit statement of relationship. Concepts are usually used within template frameworks, as a way of restricting what may be put as template parameters.
&lt;/p&gt;
&lt;p&gt;
The crucial bit is that Concepts are NOT checked by the compiler. Indeed, the compiler has no clue about Concepts. &lt;em&gt;&lt;a href=&quot;/title/They+only+exist+in+your+head&quot;&gt;They only exist in your head&lt;/a&gt;.&lt;/em&gt; There&amp;hellip;</content>
</entry><entry><title>Curiously Recurring Template Pattern (idea)</title><link rel="alternate" type="text/html" href="http://everything2.com/user/genneth/writeups/Curiously+Recurring+Template+Pattern"/><id>http://everything2.com/user/genneth/writeups/Curiously+Recurring+Template+Pattern</id><author><name>genneth</name><uri>http://everything2.com/user/genneth</uri></author><published>2005-02-22T14:40:44Z</published><updated>2005-02-22T14:40:44Z</updated>
<content type="html">&lt;p&gt;
This is a &lt;a href=&quot;/title/C%252B%252B&quot;&gt;C++&lt;/a&gt; &lt;a href=&quot;/title/idiom&quot;&gt;idiom&lt;/a&gt;, perhaps best expressed by the following:
&lt;/p&gt;
&lt;pre&gt;
template &amp;lt;typename T&amp;gt; class Base;
class Derived: public Base&amp;lt;Derived&amp;gt; {
    // stuff
&lt;/pre&gt;
&lt;p&gt;
This allows Base to reach into Derived's &lt;a href=&quot;/title/namespace&quot;&gt;namespace&lt;/a&gt; and use &lt;a href=&quot;/title/methods&quot;&gt;methods&lt;/a&gt; without the overhead of &lt;a href=&quot;/title/function+dispatch&quot;&gt;virtual dispatch&lt;/a&gt;. This also allows recursive data types to be defined.
&lt;/p&gt;
&lt;p&gt;
Examples:
&lt;/p&gt;
&lt;pre&gt;
template &amp;lt;typename T&amp;gt;
class easy_comparisons {
public:
    bool operator&amp;gt;(const T &amp;amp; rhs) {
        return rhs &amp;lt; *(static_cast&amp;lt;T *&amp;gt;this); // uses operator &amp;lt; in T's namespace
    }

    bool operator==(const T &amp;amp; rhs) {
        return !(*(static_cast&amp;lt;T *&amp;gt;this) &amp;lt; rhs) &amp;amp;&amp;amp; !(rhs &amp;lt; *(static_cast&amp;lt;T *&amp;gt;this));
    }
    // other overloads for &amp;lt;=, &amp;gt;=, != ...
};

class some_numeric : public easy_comparisons&amp;lt;some_numeric&amp;gt; {
public:
    bool operator&amp;lt;(const some_numeric &amp;amp; rhs) {
        // something...
    }
};
&lt;/pre&gt;&amp;hellip;</content>
</entry><entry><title>do { ... } while(0) (idea)</title><link rel="alternate" type="text/html" href="http://everything2.com/user/genneth/writeups/do+%257B+...+%257D+while%25280%2529"/><id>http://everything2.com/user/genneth/writeups/do+%257B+...+%257D+while%25280%2529</id><author><name>genneth</name><uri>http://everything2.com/user/genneth</uri></author><published>2005-02-22T09:39:57Z</published><updated>2005-02-22T09:39:57Z</updated>
<content type="html">&lt;p&gt;Actually, this can be used in another case, to test for errors:&lt;/p&gt;

&lt;pre&gt;do {
    if(!test1()) break;
    if(!test2()) break;
    if(!test3()) break;
    /* actual code */
} while(0);&lt;/pre&gt;

&lt;p&gt;This is pretty useless in &lt;a href=&quot;/title/C&quot;&gt;C&lt;/a&gt;, since this sort of thing is much better handled by goto (yes, yes, I know, but even the &lt;a href=&quot;/title/Linux+kernel&quot;&gt;Linux kernel&lt;/a&gt; does it). And higher level languages can use exceptions. But in &lt;a href=&quot;/title/PHP&quot;&gt;PHP&lt;/a&gt;, before version 5, this was the best way to do error testing and &lt;a href=&quot;/title/defensive+programming&quot;&gt;defensive programming&lt;/a&gt;.&lt;/p&gt;</content>
</entry><entry><title>C++: true or false? (idea)</title><link rel="alternate" type="text/html" href="http://everything2.com/user/genneth/writeups/C%252B%252B%253A+true+or+false%253F"/><id>http://everything2.com/user/genneth/writeups/C%252B%252B%253A+true+or+false%253F</id><author><name>genneth</name><uri>http://everything2.com/user/genneth</uri></author><published>2005-02-22T09:08:03Z</published><updated>2005-02-22T09:08:03Z</updated>
<content type="html">&lt;p&gt;&lt;small&gt;First, a disclaimer, this is something that I have learnt from other sources -- I can't remember where, except for the fact that I'd taken the source from the examples that it gave and used them in my own code. Please msg me about any formatting inconsistencies -- things like &amp;lt; and &amp;amp; need to be escaped to be &lt;a href=&quot;/title/E2+HTML+tags+%253A+Quick+Start&quot;&gt;valid HTML&lt;/a&gt;, and I may not have got all of them.&lt;/small&gt;&lt;/p&gt;

&lt;hr width=&quot;50%&quot;&gt;

&lt;p&gt;Ah &lt;a href=&quot;/title/Ariels&quot;&gt;Ariels&lt;/a&gt; -- if only it was that easy. Conversion to bool, as you have noted, is bad because we can do arithmetic on it. Which is not cool. But conversion to &lt;a href=&quot;/title/void+%252A&quot;&gt;void *&lt;/a&gt; allows two things that we don't want:&lt;/p&gt;

&lt;pre&gt;Testable_By_Conversion t;
delete t; // damn! that sucks!
std::cout &amp;lt;&amp;lt; t;&lt;/pre&gt;

&lt;p&gt;Yep, that's right, we can delete it, and we've got a surprise when the compiler implicitly converts it. The second one can be extended to any time when a &lt;a href=&quot;/title/void+%252A&quot;&gt;void *&lt;/a&gt; can be used, which, if it happens deep in a template library, can be really hard to pick apart.&lt;/p&gt;

&amp;hellip;</content>
</entry><entry><title>Nonlocal conservation laws (idea)</title><link rel="alternate" type="text/html" href="http://everything2.com/user/genneth/writeups/Nonlocal+conservation+laws"/><id>http://everything2.com/user/genneth/writeups/Nonlocal+conservation+laws</id><author><name>genneth</name><uri>http://everything2.com/user/genneth</uri></author><published>2003-10-10T14:30:22Z</published><updated>2003-10-10T14:30:22Z</updated>
<content type="html">&lt;p&gt;Most of physics becomes easier as soon as you realise that some things are always the same: energy, mass, charge, etc. Thus we tend to think of these quantities as flowing from one place to another, i.e. if it disappears from here in this instant, it will be somewhere very closeby in the next instant. However, a brief &lt;a href=&quot;/title/thought+experiment&quot;&gt;thought experiment&lt;/a&gt; will reveal otherwise. Consider two masses suspended over a pulley in a gravitational field -- then a change of position (and potential energy) in one will be transferred (potentially) to somewhere entirely different.&lt;/p&gt;

&lt;p&gt;But how local is local? &lt;a href=&quot;/title/Special+relativity&quot;&gt;Special relativity&lt;/a&gt; can be interpreted to say that nothing, including information, can travel faster than the speed of light. So in the experiment above, the effect is still local -- the speed at which the propagation of a mechanical wave through whatever medium the two masses were joined is still less than the speed of light. We could say that the &quot;string&quot; took up our slack.&lt;/p&gt;

&lt;p&gt;So really, in our current context,&amp;hellip;</content>
</entry></feed>
