为什么某些C++标准库“运算符<<”函数将其流宽度重置为 0

Why do some C++ standard library `operator<<` functions reset their streams width to 0?

本文关键字:运算符 函数 C++ 为什么 标准      更新时间:2023-10-16

如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)的呼吁?

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

这里有一个相关的问题,它证明了这造成的混乱,但没有解释为什么标准规定这种行为。

它似乎不是"一些"operator<<函数,一般来说 插入器和提取器 (21.4.8.9)。参见 C++ 标准和 Tony D 答案中的合理推理