【问题标题】:Does std::endl work with both cout and wcout?std::endl 是否适用于 cout 和 wcout?
【发布时间】:2012-12-01 13:16:07
【问题描述】:

我想知道 std::endl 是否同时适用于 std::cout 和 std::wcout?

有人清楚吗?

【问题讨论】:

  • 写代码测试一下怎么样?
  • 我猜他的意思是问他使用 endl 和 wcout 是否有什么问题。
  • 我添加了更多细节来解释机械手的工作原理。

标签: c++


【解决方案1】:

是的。事实上,std::endl 是一个函数模板,可以作为 std::basic_ostream 模板的任何特化的操纵器。


更多细节:27.7.3.6 规定std::basic_ostream 模板包含operator<< 的重载,如下所示:

basic_ostream<charT, traits> &
operator<<(basic_ostream<charT, traits> (*pf)(basic_ostream<charT, traits> &));

在合适的函数上调用这个重载的效果是return pf(*this)。所以当你说std::cout &lt;&lt; std::endl时,它实际上变成了std::endl(std::cout),并返回一个对流对象的引用。

所有其他 ostream 操纵器的编写方式相同,输入操纵器也类似。


endl 函数模板的神奇之处在于调用widen('\n'),它为给定的字符类型生成正确的“换行符”数据。

【讨论】:

    猜你喜欢
    • 2021-08-04
    • 2015-11-27
    • 1970-01-01
    • 2017-05-13
    • 2020-03-19
    • 2018-05-07
    • 1970-01-01
    • 2012-01-08
    相关资源
    最近更新 更多