Suggest a feature

Welcome to TagMyCode

Please login or create account to add a snippet.
Language: C++
Posted by: Jose Fernando Lopez Fernandez
Added: May 17, 2018 2:13 PM
Modified: May 31, 2018 2:17 AM
Views: 34
The standard library std::endl operation does two things: it prints a new line and then it calls std::flush, flushing the output stream buffer. This is not ideal because the stream is already flushed either when the buffer is full or when a call is made to std::cin. It's usually best to let the buffers be handled by the system in place, so this replacement replaces std::endl with NL. This way it is still semantically clear that a new line is being output to the output stream, but no call to std::flush is being made.
  1. template <typename CharT, typename Traits = std::char_traits<CharT>>
  2. std::basic_ostream<CharT, Traits>&
  3. NL (std::basic_ostream<CharT, Traits>& outputStream)
  4. {
  5.         return outputStream << outputStream.widen('\n');
  6. }