【发布时间】: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