2

In C++, when I do

std::cout << 1.2;

what is the actual chain of functions that are called to actually display the number? I realize this is compiler specific, but I am interested in particular with the Gnu libstdc++ implementation.

In C, calling printf delegates to vfprintf, which through jump tables calls __printf_fp in glibc. I'm looking for an analogous chain in the pure C++ setting.

Victor Liu
  • 3,545
  • 2
  • 24
  • 37
  • As you said I realize this is compiler specific. Not only that but version specific. This kind of information is not useful. – Martin York Oct 02 '12 at 23:28
  • Really any version would do. The point is not precision but rather to get an idea of which library layers the functions tunnel through. I have a vague impression that `locale` is involved. – Victor Liu Oct 02 '12 at 23:51

1 Answers1

2

Clearly, it will call ostream::operator<< first but it may be library specific beyond that. The best way to answer this is to debug into the code and follow the functions as they occur. This will not only tell you what functions are called but tell your about edge cases and error handling that occurs. Looking at the code may help but it is likely convoluted.

Using this code:

std::cout << 1.2f;

... here is what is does in Visual Studio 2012 without the noise:

  1. operator<<(float _Val) (std::basic_ostream<_Elem, _Traits>)
    1. Initialize a state variable to good.
    2. Call use_facet<_Facet>(const locale & _Loc) (std) to get the num_put facet.
    3. Call num_put(_OutIt _Dest, ios_base& _Iosbase, _Elem _Fill, double _Val) (std) to write the float to the output stream (converted to a double) using local specific formatting. Internally, this:
      1. Checks the precision, such fixed, to see if the default formatting has been modified.
      2. Formats the number and writes it to the output as characters to the current iterator used by the stream.
    4. Set the state variable to bad if the operation failed.
    5. Call setstate to the state.

So most of the work is actually done in the num_put facet, which writes to an iterator for the output stream.

akton
  • 14,148
  • 3
  • 43
  • 47
  • Compiling and running a simple program with debugging does not allow me to step into the library routines. – Victor Liu Oct 02 '12 at 23:52
  • If your compiler or library does not allow debugging into it, perhaps look at the source code, such as http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-3.4/ostream-source.html. – akton Oct 02 '12 at 23:54
  • Don't you think I've tried that? The source code for the standard C++ libraries is incredibly convoluted, and the only reason I'm asking the question in the first place is because I'm hoping there's an expert here that can give me a bit of guidance. Right now, I'm tracing operator<< back to the use of facets, which is obtained from a locale object... and I'm not even sure I'm interpreting what I see correctly. – Victor Liu Oct 03 '12 at 00:02
  • I understand your frustration. I am no STL expert but I have looked through the code in Visual Studio and attempted to give more detail. What you have described in the comment above is correct. – akton Oct 03 '12 at 12:00