Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Solution: When coding a comparison, have the return type be a <code>comparison</code> object that can chain additional comparisons, but is implicitly convertible to a <code>bool</code>. This can even (kind of) work with types that weren't coded with this intent, simply by casting them to the <code>comparison</code> type manually.</p> <p>Implementation:</p> <pre><code>template&lt;class T&gt; class comparison { const bool result; const T&amp; last; public: comparison(const T&amp; l, bool r=true) :result(r), last(l) {} operator bool() const {return result;} comparison operator&lt;(const T&amp; rhs) const {return comparison(rhs, (result &amp;&amp; last&lt;rhs));} comparison operator&lt;=(const T&amp; rhs) const {return comparison(rhs, (result &amp;&amp; last&lt;=rhs));} comparison operator&gt;(const T&amp; rhs) const {return comparison(rhs, (result &amp;&amp; last&gt;rhs));} comparison operator&gt;=(const T&amp; rhs) const {return comparison(rhs, (result &amp;&amp; last&gt;=rhs));} }; </code></pre> <p>A useful example:</p> <pre><code>#include &lt;iostream&gt; int main() { //testing of chained comparisons with int std::cout &lt;&lt; (comparison&lt;int&gt;(0) &lt; 1 &lt; 2) &lt;&lt; '\n'; std::cout &lt;&lt; (comparison&lt;int&gt;(0) &lt; 1 &gt; 2) &lt;&lt; '\n'; std::cout &lt;&lt; (comparison&lt;int&gt;(0) &gt; 1 &lt; 2) &lt;&lt; '\n'; std::cout &lt;&lt; (comparison&lt;int&gt;(0) &gt; 1 &gt; 2) &lt;&lt; '\n'; } </code></pre> <p>Output:</p> <pre><code>1 0 0 0 </code></pre> <p>Note: This was created by <a href="https://stackoverflow.com/users/845092/mooing-duck">Mooing Duck</a>, and a compiled, more robust example can be found on <a href="http://ideone.com/awrmK" rel="nofollow noreferrer">http://ideone.com/awrmK</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload