Does std::endl work with both cout and wcout?

c++

Solution

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.

Problem

I am wondering if std::endl works with both std::cout and std::wcout? Anyone is clear on this?

Original source