我正在查看的旧代码中,std::endl
后面紧跟着 std::flush
的情况非常普遍。一开始,我认为这是多余的,因为我在以下链接描述的 std::endl
和 std::flush
中看到了:
http://zh.cppreference.com/w/cpp/io/manip/endl
http://zh.cppreference.com/w/cpp/io/manip/flush
以下是我在旧源代码中看到的示例:
std::cout << "Generic Notification" << std::endl << std::flush;
然而,许多资深
软件开发人员在这些年中已经看过这段代码,我想知道是否有一些细节我错过了。在std::endl
之后使用std::flush
是否有任何用处?
std::cout
而是其他流,可能有原因... - 463035818_is_not_a_numberstd::endl
被定义为“换行并刷新缓冲区”。 - Angew is no longer proud of SOstd::cout
。当然,即使对于类似于自定义流的对象,在那个位置使用flush
也没有任何用处。 - 463035818_is_not_a_numberflush
,但这仍然不是一个好的理由,因为在endl
上不刷新的流可以被认为是损坏的。 - 463035818_is_not_a_number