Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default `fill character` of std::stringstream?

Is it implementation defined or standards suggest a default fill character for streams?

Sample code:

#include <iostream>
#include <iomanip>
#include <sstream>

int main ()
{
    std::stringstream stream;
    stream << std::setw( 10 ) << 25 << std::endl;

    std::cout << stream.str() << std::endl;
}

With clang++ --stdlib=libstdc++

$ clang++ --stdlib=libstdc++ test.cpp
$ ./a.out | hexdump
0000000 20 20 20 20 20 20 20 20 32 35 0a 0a
000000c
$

With clang++ --stdlib=libc++

$ clang++ --stdlib=libc++ test.cpp
$ ./a.out | hexdump
0000000 ff ff ff ff ff ff ff ff 32 35 0a 0a
000000c

Version

$ clang++ --version
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix

I was able to fix it with std::setfill(' '), but am curious to know if it is a clang bug.

like image 824
Vikas Avatar asked Dec 27 '13 01:12

Vikas


People also ask

What is std :: Stringstream?

Stream class to operate on strings. Objects of this class use a string buffer that contains a sequence of characters. This sequence of characters can be accessed directly as a string object, using member str .

What does Stringstream return in C++?

std::stringstream::str The first form (1) returns a string object with a copy of the current contents of the stream. The second form (2) sets s as the contents of the stream, discarding any previous contents.

What type is Stringstream?

A stringstream class in C++ is a Stream Class to Operate on strings. The stringstream class Implements the Input/Output Operations on Memory Bases streams i.e. string: The stringstream class in C++ allows a string object to be treated as a stream. It is used to operate on strings.

What does Setfill mean in C++?

The setfill() method of iomanip library in C++ is used to set the ios library fill character based on the character specified as the parameter to this method. Syntax: setfill(char c) Parameters: This method accepts c as a parameter which is the character argument corresponding to which the fill is to be set.


1 Answers

The default fill character for a stream s is s.widen(' ') according to 27.5.5.2 [basic.ios.cons] paragraph 3/Table 128. What character results from s.widen(' ') is, however, dependent on the std::locale as s.widen(c) is (27.5.5.3 [basic.ios.members] paragraph 12):

std::use_facet<std::ctype<cT>>(s.getloc()).widen(c)

BTW, you should use std::ostringstream when you are only writing to the stream and there is no use of std::endl ever.

like image 158
Dietmar Kühl Avatar answered Sep 27 '22 23:09

Dietmar Kühl