Bug 75112

Summary: std::iomanip and std::ostream not working correctly for std::string
Product: [Retired] Red Hat Linux Reporter: Tad Marko <tad>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED NEXTRELEASE QA Contact: Brian Brock <bbrock>
Severity: medium Docs Contact:
Priority: medium    
Version: 8.0   
Target Milestone: ---   
Target Release: ---   
Hardware: i386   
OS: Linux   
URL: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=upmn61ssqkeg0e%40corp.supernews.com&rnum=1&prev=/groups%3Fq%3D%2522tad%2Bmarko%2522%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26scoring%3Dd
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2004-10-02 00:09:52 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Tad Marko 2002-10-04 16:30:09 UTC
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.0) Gecko/20020530

Description of problem:
Using std::iomanip on std::ostream with std::string to pad output does not work
the same as it does for char*.

Version-Release number of selected component (if applicable):


How reproducible:
Always

Steps to Reproduce:
#include <iostream>
#include <iomanip>
#include <sstream>

using namespace std;

main()
{
    char*  c("CHAR");
    string s("STRING");

    {
        ostringstream buffer;
        cout << "In a 10 char wide field:\n";
        buffer << "|" << setw(10) << c << "|";
        cout << buffer.str() << "\t";
    }
    {
        ostringstream buffer;
        buffer << "|" << setw(10) << s << "|";
        cout << buffer.str();
    }

    cout << "\n\n";

    {
        ostringstream buffer;
        cout << "In a 10 char wide field, left justified:\n";
        buffer << "|";
        buffer.setf(ios::left);
        buffer << setw(10) << c << "|";
        cout << buffer.str() << "\t";
    }
    {
        ostringstream buffer;
        buffer << "|";
        buffer.setf(ios::left);
        buffer << setw(10) << s << "|";
        cout << buffer.str();
    }

    cout << "\n\n";

    {
        ostringstream buffer;
        cout << "In a 10 char wide field, left justified, zero
padded:\n";
        buffer << "|";
        buffer.setf(ios::left);
        buffer << setw(10) << setfill('0') << c << "|";
        cout << buffer.str() << "\t";
    }
    {
        ostringstream buffer;
        buffer << "|";
        buffer.setf(ios::left);
        buffer << setw(10) << setfill('0') << s << "|";
        cout << buffer.str();
    }

    cout << "\n\n";
}

Actual Results:  In a 10 char wide field:
|      CHAR|	|STRING         |
char* and std::string output do not match:

In a 10 char wide field, left justified:
|CHAR      |	|STRING|         

In a 10 char wide field, left justified, zero padded:
|CHAR000000|
|STRING|000000000

Expected Results:  char* and std::string output should be padded similarly.

Additional info:

Before entering this bug here, I posted to comp.lang.c++ and my first response
was from P.J. Plauger himself, who agrees this is aberrant behavior. The
attached URL is for the thread for that post on Google Groups.

Comment 1 Benjamin Kosnik 2004-10-02 00:09:52 UTC
Fixed 3.2.3, 3.3.x, 3.4.x.