Bug 28278 - formatted I/O and calling width(0)
Summary: formatted I/O and calling width(0)
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-07-05 23:34 UTC by Paolo Carlini
Modified: 2023-12-02 00:33 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-10-08 01:17:34


Attachments
Martin's test code (1.41 KB, text/plain)
2023-12-02 00:33 UTC, Jonathan Wakely
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Paolo Carlini 2006-07-05 23:34:09 UTC
This is an internal reminder for the issue pointed out by Martin in:

  c++std-lib-17244

It looks like, we are implementing the current C++ specifications rather well, besides a few issues. See the test:

  http://people.apache.org/~sebor/width_test.cpp
Comment 1 Michael 2010-12-27 11:25:51 UTC
Can anybody fix this bug?

------- code: -------
#include<iostream>
using namespace std;

int main()
{
  cout.setf(ios_base::fixed);
  cout.precision(3);
  cout.width(20);
  cout.fill('#');

  cout << (double)1.123456789 << endl;
  cout << (double)0.123456789 << endl;
  cout << (double)10.123456789 << endl;
}
---------------------

------- output: -------
###############1.123
0.123
10.123
-----------------------

------- expected: -------
###############1.123
###############0.123
##############10.123
-------------------------

It's just sad... :^(((
Comment 2 Paolo Carlini 2010-12-27 12:12:13 UTC
The snippet you posted has nothing to do with the issue discussed here, which is about *when exactly* calling width(0) toward the end of the insertion operation (there are minor inconsistencies in the Standard detectable when the insertion proper throws). But in any case, per the letter of both C++03 and C++0x , during the arithmetic insertion at some point width(0) has to be called, normally by the num_put facet in Stage 3. Thus the behavior we are implementing vs your snippet is already Ok, no bug to fix (if you want a practical check, Sun Studio behaves the same).
Comment 3 Jonathan Wakely 2010-12-27 14:31:39 UTC
(In reply to comment #1)
> Can anybody fix this bug?

Don't be sad, your bug is easy to fix:
 
> ------- code: -------
> #include<iostream>
> using namespace std;
> 
> int main()
> {
>   cout.setf(ios_base::fixed);
>   cout.precision(3);
>   cout.width(20);
>   cout.fill('#');
> 
   cout << std::setw(20) << 1.123456789 << endl;
   cout << std::setw(20) << 0.123456789 << endl;
   cout << std::setw(20) << 10.123456789 << endl;
> }

Fixed.
Comment 4 Michael 2010-12-27 15:12:50 UTC
:))OK.
( I merely have looked at 'ios_base state functions' chapter in C++0x draft and
not found any mention that any preset state of a stream may be lost after an
output operation.  It turns out that the width is the (only) state which can
be(inconsequently) lost after an output operation. )
Comment 5 Jonathan Wakely 2023-12-02 00:33:21 UTC
Created attachment 56765 [details]
Martin's test code

Attaching the test code here, in case the link stops working.