Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Important notes from the comments below:</p> <p>By Martin: </p> <blockquote> <p>@Chareles: Then by this requirement all manipulators are sticky. Except setw which seems to be reset after use.</p> </blockquote> <p>By Charles: </p> <blockquote> <p>Exactly! and the only reason that setw appears to behave differently is because there are requirements on formatted output operations to explicitly .width(0) the output stream.</p> </blockquote> <p>The following is the discussion that lead to the above conclusion:</p> <hr> <p>Looking at the code the following manipulators return an object rather than a stream:</p> <pre><code>setiosflags resetiosflags setbase setfill setprecision setw </code></pre> <p>This is a common technique to apply an operation to only the next object that is applied to the stream. Unfortunately this does not preclude them from being sticky. Tests indicate that all of them except <code>setw</code> are sticky.</p> <pre><code>setiosflags: Sticky resetiosflags:Sticky setbase: Sticky setfill: Sticky setprecision: Sticky </code></pre> <p>All the other manipulators return a stream object. Thus any state information they change must be recorded in the stream object and is thus permanent (until another manipulator changes the state). Thus the following manipulators must be <strong>Sticky</strong> manipulators.</p> <pre><code>[no]boolalpha [no]showbase [no]showpoint [no]showpos [no]skipws [no]unitbuf [no]uppercase dec/ hex/ oct fixed/ scientific internal/ left/ right </code></pre> <p>These manipulators actually perform an operation on the stream itself rather than the stream object (Though technically the stream is part of the stream objects state). But I do not believe they affect any other part of the stream objects state.</p> <pre><code>ws/ endl/ ends/ flush </code></pre> <p>The conclusion is that setw seems to be the only manipulator on my version that is not sticky.</p> <p>For Charles a simple trick to affect only the next item in the chain:<br> Here is an Example how an object can be used to temporaily change the state then put it back by the use of an object:</p> <pre><code>#include &lt;iostream&gt; #include &lt;iomanip&gt; // Private object constructed by the format object PutSquareBracket struct SquareBracktAroundNextItem { SquareBracktAroundNextItem(std::ostream&amp; str) :m_str(str) {} std::ostream&amp; m_str; }; // New Format Object struct PutSquareBracket {}; // Format object passed to stream. // All it does is return an object that can maintain state away from the // stream object (so that it is not STICKY) SquareBracktAroundNextItem operator&lt;&lt;(std::ostream&amp; str,PutSquareBracket const&amp; data) { return SquareBracktAroundNextItem(str); } // The Non Sticky formatting. // Here we temporariy set formating to fixed with a precision of 10. // After the next value is printed we return the stream to the original state // Then return the stream for normal processing. template&lt;typename T&gt; std::ostream&amp; operator&lt;&lt;(SquareBracktAroundNextItem const&amp; bracket,T const&amp; data) { std::ios_base::fmtflags flags = bracket.m_str.flags(); std::streamsize currentPrecision = bracket.m_str.precision(); bracket.m_str &lt;&lt; '[' &lt;&lt; std::fixed &lt;&lt; std::setprecision(10) &lt;&lt; data &lt;&lt; std::setprecision(currentPrecision) &lt;&lt; ']'; bracket.m_str.flags(flags); return bracket.m_str; } int main() { std::cout &lt;&lt; 5.34 &lt;&lt; "\n" // Before &lt;&lt; PutSquareBracket() &lt;&lt; 5.34 &lt;&lt; "\n" // Temp change settings. &lt;&lt; 5.34 &lt;&lt; "\n"; // After } &gt; ./a.out 5.34 [5.3400000000] 5.34 </code></pre>
 

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