Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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

Anyone is clear on this?

like image 706
Adam Lee Avatar asked Dec 01 '12 13:12

Adam Lee


People also ask

Is std :: endl necessary?

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.

What is the difference between Cout and Wcout?

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.

How does STD Endl work?

std::endl. Inserts a new-line character and flushes the stream. Its behavior is equivalent to calling os.

Should I use std :: endl or \n?

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 .


1 Answers

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.

like image 106
Kerrek SB Avatar answered Oct 05 '22 23:10

Kerrek SB