Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason that <code>width</code> does not appear to be 'sticky' is that certain operations are guaranteed to call <code>.width(0)</code> on an output stream. Those are:</p> <p>21.3.7.9 [lib.string.io]:</p> <pre><code>template&lt;class charT, class traits, class Allocator&gt; basic_ostream&lt;charT, traits&gt;&amp; operator&lt;&lt;(basic_ostream&lt;charT, traits&gt;&amp; os, const basic_string&lt;charT,traits,Allocator&gt;&amp; str); </code></pre> <p>22.2.2.2.2 [lib.facet.num.put.virtuals]: All <code>do_put</code> overloads for the <code>num_put</code> template. These are used by overloads of <code>operator&lt;&lt;</code> taking a <code>basic_ostream</code> and a built in numeric type.</p> <p>22.2.6.2.2 [lib.locale.money.put.virtuals]: All <code>do_put</code> overloads for the <code>money_put</code> template.</p> <p>27.6.2.5.4 [lib.ostream.inserters.character]: Overloads of <code>operator&lt;&lt;</code> taking a <code>basic_ostream</code> and one of the char type of the basic_ostream instantiation or <code>char</code>, signed <code>char</code> or <code>unsigned char</code> or pointers to arrays of these char types.</p> <p>To be honest I'm not sure of the rationale for this, but no other states of an <code>ostream</code> should be reset by formatted output functions. Of course, things like <code>badbit</code> and <code>failbit</code> may be set if there is a failure in the output operation, but that should be expected.</p> <p>The only reason that I can think of for resetting the width is that it might be surprising if, when trying to output some delimited fields, your delimiters were padded.</p> <p>E.g.</p> <pre><code>std::cout &lt;&lt; std::setw(6) &lt;&lt; 4.5 &lt;&lt; '|' &lt;&lt; 3.6 &lt;&lt; '\n'; " 4.5 | 3.6 \n" </code></pre> <p>To 'correct' this would take:</p> <pre><code>std::cout &lt;&lt; std::setw(6) &lt;&lt; 4.5 &lt;&lt; std::setw(0) &lt;&lt; '|' &lt;&lt; std::setw(6) &lt;&lt; 3.6 &lt;&lt; std::setw(0) &lt;&lt; '\n'; </code></pre> <p>whereas with a resetting width, the desired output can be generated with the shorter:</p> <pre><code>std::cout &lt;&lt; std::setw(6) &lt;&lt; 4.5 &lt;&lt; '|' &lt;&lt; std::setw(6) &lt;&lt; 3.6 &lt;&lt; '\n'; </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. 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.
    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