10

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

Anyone is clear on this?

Adam Lee
  • 23,314
  • 47
  • 144
  • 221

1 Answers1

15

Yes. In fact, std::endl 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.

Kerrek SB
  • 447,451
  • 88
  • 851
  • 1,056