What is the C++ iostream endl fiasco?

孤人 提交于 2019-11-26 01:08:13

问题


I was listening to a google talk by Andrei Alexandrescu on the D programming language when he threw out a one liner about the \"endl\" fiasco. I just thought endl was the preferred way to signify the end of a line and flush the buffer for a stream. Why is it considered a fiasco? Should I not be using it in my code?


回答1:


Reposting from my comment:

(I assume) He just means that many, especially new, C++ programmers use std::endl blindly instead of '\n' for newline, flushing unnecessarily frequently and potentially making the performance of their program abysmal.

I.e., most people are taught that std::endl is the canonical way to insert a newline into a stream even though it is very rarely necessary or appropriate to flush it.

It is some people's opinion (*cough*) that std::endl shouldn't even be in the standard, as it is so rarely appropriate and not a significant typing savings over '\n' << std::flush anyway.


TL;DR(s):

  • In the end, std::endl buys you nothing except usually worse performance and usually more typing.
  • It has its uses, but they are considerably rarer than its frequency of use in most large codebases would suggest, therefore...
  • Its utility is highly questionable and its ubiquity is absurd – a fiasco indeed!


来源:https://stackoverflow.com/questions/5492380/what-is-the-c-iostream-endl-fiasco

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!