【问题标题】:Why do some C++ standard library `operator<<` functions reset their streams width to 0?为什么某些 C++ 标准库 `operator<<` 函数会将其流宽度重置为 0?
【发布时间】:2014-02-25 05:02:05
【问题描述】:

如 C++ 标准的 [ostream.inserters.character] 部分所述,在将 char 或字符串插入流后,流的宽度设置为 0:

template<class traits>
  basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>& out,
                                         const unsigned char* s);
Do lots of sensible and expected things...
Calls width(0).

为什么 C++ 标准要求调用 width(0)

更改流宽度而不将其重置为原始值的原因是什么? (据我所知,流的所有其他属性都由流插入运算符保留。)

有一个相关的问题here,它说明了由此引起的混乱,但没有解释为什么标准规定了这种行为。

【问题讨论】:

  • 我不想写答案,因为只有参与这个决定的人才能说,但我很确定原因是方便。您不想在多个输出中维护width 的情况远远超过您这样做的情况。
  • This answer 包含一个猜测

标签: c++ c++11 iostream language-lawyer


【解决方案1】:

这似乎不是“一些”operator&lt;&lt; 函数,一般来说是插入器和提取器 (21.4.8.9)。请参阅 c++ standardTony D's answer 中的合理推理

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 2016-07-28
    • 2021-08-31
    相关资源
    最近更新 更多