Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tested it.</p> <p>I can guess that <code>operator&lt;&lt;</code> cannot bind a temporary to a non-const reference, so any externally defined operator&lt;&lt; functions will not work on the Foo temporary, but any class member ones will so if <code>ostream</code> or <code>ostringstream</code> has any internal <code>operator&lt;&lt;</code> members they will work.</p> <p>Therefore it may be that the overload to a pointer is a member function whilst the special one for const char * is externally declared.</p> <p>The non-temporary can bind to the non-const reference for the more specialist overload.</p> <p>If you really need this you can workaround with a wrapper</p> <pre><code>class Foo : { mutable std::ostringstream oss; public: ~Foo() { std::cout &lt;&lt; oss.str(); } template&lt;typename T&gt; std::ostream&amp; operator&lt;&lt;( const T&amp; t ) const { return oss &lt;&lt; t; } }; </code></pre> <p>Tested and works. The first operator&lt;&lt; will return you the underlying stream.</p> <p>I tried this too but it coredumped:</p> <pre><code>class Foo : std::ostringstream { Foo &amp; nonconstref; public: Foo() : nonconstref( *this ) {} ~Foo() { std::cout &lt;&lt; str(); } template&lt;typename T&gt; std::ostream&amp; operator&lt;&lt;( const T&amp; t ) const { return nonconstref &lt;&lt; t; } }; </code></pre> <p>This also works:</p> <pre><code>class Foo : public std::ostringstream { public: Foo() {} ~Foo() { std::cout &lt;&lt; str(); } Foo&amp; ncref() { return *this; } }; int main() { Foo foo; foo &lt;&lt; "Test1" &lt;&lt; std::endl; Foo().ncref() &lt;&lt; "Test2" &lt;&lt; std::endl; } </code></pre>
    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