C ++输出流浮点调用链 [英] C++ output stream floating point call chain

查看:72
本文介绍了C ++输出流浮点调用链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,当我这样做

In C++, when I do

std::cout << 1.2;

被调用以实际显示数字的实际函数链是什么?我意识到这是特定于编译器的,但是我特别对Gnu libstdc ++实现感兴趣.

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.

在C中,调用printf委托给vfprintf,后者通过跳转表调用glibc中的__printf_fp.我正在寻找纯C ++设置中的类似链.

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.

推荐答案

很显然,它将调用 ostream :: operator<< 首先,但它可能是特定于库的.回答此问题的最佳方法是调试代码并按照功能出现的顺序进行操作.这不仅会告诉您调用了什么函数,而且还会告诉您有关极端情况和发生的错误处理的信息.查看代码可能会有所帮助,但可能会令人费解.

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.

使用此代码:

std::cout << 1.2f;

...这是在Visual Studio 2012中没有噪音的情况:

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

  1. operator<<(float _Val)( std :: basic_ostream< _Elem,_Traits>)
  1. operator<<(float _Val) (std::basic_ostream<_Elem, _Traits>)
  1. state 变量初始化为 use_facet< __ Facet>(const locale& _Loc)( std)获取num_put构面.
  2. 致电 num_put(_OutIt _Dest,ios_base& _Iosbase,_Elem _Fill,双_Val )(std)使用本地特定格式将浮点数写入输出流(转换为双精度).在内部,这是:
  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. 检查精度,例如
  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.

  • 如果操作失败,则将状态变量设置为 bad .
  • setstate 设置为该状态.
  • Set the state variable to bad if the operation failed.
  • Call setstate to the state.
  • 因此,大多数工作实际上是在num_put方面完成的,该方面将写入输出流的迭代器.

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

    这篇关于C ++输出流浮点调用链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆