Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you write </p> <pre><code> os &lt;&lt; tuple_.get&lt;0&gt;(); </code></pre> <p>there is no function that matches both parameters. Instead the compiler has a choice to apply an implicit conversion on either parameter</p> <pre><code>std::ostream &lt;&lt; int </code></pre> <p>or</p> <pre><code>std::ostringstream &lt;&lt; MyTuple </code></pre> <p>The latter would happen with the <code>boost::tuple</code> constructor that can take any number of arguments up to number of tuple elements. (And with <code>float</code> it fails, because <code>float</code> is convertible to <code>int</code>.)</p> <p>When overloading stream operators, use the base class as the left hand side (<code>ostream</code> or even <code>basic_ostream&lt;CharT, Traits&gt;</code>.</p> <hr> <p><strong>Edit:</strong> You could disambiguate the call by casting the <strong>first</strong> argument.</p> <pre><code>ostringstream&amp; operator&lt;&lt;(ostringstream&amp; os_, Mytuple tuple_) { static_cast&lt;std::ostream&amp;&gt;(os_) &lt;&lt; tuple_.get&lt;0&gt;(); static_cast&lt;std::ostream&amp;&gt;(os_) &lt;&lt; tuple_.get&lt;1&gt;(); static_cast&lt;std::ostream&amp;&gt;(os_) &lt;&lt; tuple_.get&lt;2&gt;(); // Error because no matching operator. Fine. return os_; } </code></pre> <p>However, overloading the operator with <code>ostringstream</code> is still a bad idea, because it won't work with operator chaining.</p> <pre><code>MyTuple a, b; ostringstream ss; ss &lt;&lt; a &lt;&lt; ' ' &lt;&lt; b; </code></pre> <p>will invoke:</p> <p>1) <code>ostringstream&amp; operator&lt;&lt;(ostringstream&amp; os_, Mytuple tuple_)</code></p> <p>2) <code>ostream&amp; ostream::operator&lt;&lt;(char)</code></p> <p>3) <strong><code>ostream&amp; operator&lt;&lt;(ostream&amp;&amp;, boost::tuple&lt;int,float,Myclass&gt;</code></strong></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. 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