c++ - 为什么某些 C++ 标准库 `operator<<` 函数将其流宽度重置为 0?

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

如 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 ,这说明了这引起的困惑,但没有解释为什么标准规定了这种行为。

最佳答案

好像不是“一些”operator<<功能,一般来说是插入器和提取器 (21.4.8.9)。参见 c++ standard和一个合理的推理 Tony D's answer

关于c++ - 为什么某些 C++ 标准库 `operator<<` 函数将其流宽度重置为 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22005035/

相关文章:

c++ - 如何检查类中是否调用了宏?

c++ - 要使用的类初始化括号

c++ - 获取 std::map 中的最小键

c++错误错误C2512没有合适的默认构造函数可用

c++ - Signed Char的目的是什么

c++11 - 自旋锁实现推理

c++ - C++ 编码人员通常会显式地进行 move ,还是只依赖编译器来完成?

C++ iostream >> 运算符的行为不同于 get() unsigned char

c++ - cout doubles 不切割变量

c++ - 为什么 std::ostream 的构造函数受到保护?