10

I am wondering if std::endl works with both std::cout and std::wcout?

Anyone is clear on this?

Adam Lee
  • 24,710
  • 51
  • 156
  • 236

1 Answers1

18

Yes. In fact, std::endl is a function template that will work as a manipulator on any specialization of the std::basic_ostream template.


Some more detail: 27.7.3.6 prescribes that the std::basic_ostream template contain overload for operator<< as follows:

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

The effect of invoking this overload on a suitable function is return pf(*this). So when you say std::cout << std::endl, this actually becomes std::endl(std::cout) and returns a reference to the stream object.

All other ostream manipulators are written in the same way, and similarly for input manipulators.


The magic of the endl function template is a call to widen('\n'), which produces the correct "newline" data for the given character type.

Leon
  • 31,443
  • 4
  • 72
  • 97
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084