C++ 如何重置非粘性格式属性?

C++ 如何重置非粘性格式属性?,c++,output,output-formatting,C++,Output,Output Formatting,考虑以下代码: class C{}; std::ostream &operator <<(std::ostream &o, const C &) { o.fill('!'); o.width(8); return o << 42; } int main() { std::cout << C{} << '\n'; std::cout << 42 << '\n'

考虑以下代码:

class C{};

std::ostream &operator <<(std::ostream &o, const C &) {
    o.fill('!');
    o.width(8);
    return o << 42;
}

int main() {
    std::cout << C{} << '\n';
    std::cout << 42 << '\n';
    return 0;
}
C类{};

std::ostream&operator如下划线所述:
width
没有粘性。但实际上,iostream类及其操纵器没有这样的属性
粘性


尽管如此,如果之前的
不会重置宽度,则填充
是粘性的<代码>宽度
不可用。您看不到
填充
字符,因为它们没有填充到(现在重置)
宽度
。可能重复的
宽度以及如何重置
宽度
?这些都有很好的文档记录。上面的链接记录了哪些修饰符是粘性的,并提供了为什么
width
不是粘性的可能理由。如果您想知道它是如何复位的,请深入研究编译器标准库的代码。如果你想做你自己的覆盖,试着看看:Thank's,但是如果没有这个例子,答案就不值得一个“答案”。
std::cout << std::setw(10) << std::setfill('!') << 42 << '\n';
std::cout << std::setw(10) << std::setfill('!') << 42 << std::setw(10) << '\n';
!!!!!!!!42
!!!!!!!!42!!!!!!!!!