I am wondering if std::endl works with both std::cout and std::wcout?
Anyone is clear on this?
There is rarely a need for std::endl , and getting in the habit of using it will lead to mysteriously slow code. Just use '\n' unless you know you need to flush the buffer.
Difference between wcout and coutcout users char (narrow character) as character type. It can be used for ASCII and ANSI characters. For internationalization, we need Unicode strings which do not fit in char. wcout uses wchar_t (wide character) and usable for Unicode characters.
std::endl. Inserts a new-line character and flushes the stream. Its behavior is equivalent to calling os.
The only difference is that std::endl flushes the output buffer, and '\n' doesn't. If you don't want the buffer flushed frequently, use '\n' . If you do (for example, if you want to get all the output, and the program is unstable), use std::endl .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With