7

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.

Vikas
  • 8,790
  • 4
  • 38
  • 48

1 Answers1

6

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.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • Thanks. So it looks like a bug with implementation. `s.widen(' ')` with both `libstdc++` and `libc++` return `0x20`, which is not what fill char for stream is with `libc++`. – Vikas Dec 27 '13 at 04:47
  • 1
    Looks like there already is a [bug filed](http://llvm.org/bugs/show_bug.cgi?id=17604) and fixed. – Vikas Dec 27 '13 at 05:10