Jonathan Mcdougall said:
This is a personal opinion.
Agreed.
I like the look of
std::cout << "hey" << std::endl;
better than
std::cout << "hey\n";
But note that the code one writes is quite often
read by others. This is imo good reasons to
follow 'convention'. Consistency Is Good(tm).
Also note that since 'endl' does cause a flush,
you're possibly slowing down the program, often
unnecessarily. This might not be noticable in some
cases, but could significantly impact performance
in others. E.g. writing 'endl' to an ofstream
attached to a file on a floppy disk, or other
inherently 'slow' device, such as a printer.
Ok let me rephrase that
Flushing will send the stream's content to stdout,
Flushing will send any data not yet written to the
device to which the stream is attached.
but the operating
system is free to do whatever it wants with it.
You're outside the language again. One cannot
comment further about 'behavior' at that point
from a language perspective.
We're in a language perspective in this group. OS buffers
and their behavior are outside that domain.
"All streams" means all stream types, input, output,
or 'udpate' (input&output). Flushing is only defined
for output and 'update' streams.
Perhaps you were only being sloppy with terminology
and meant 'all output streams'. Which then would
omit 'update' streams.
woaa
I think we agree that after a buffer
make that 'stream', and by implication, that stream's
'stream buffer'. 'stream buffer' is a language construct,
not part of an OS.
has been flushed, the
operating system is free to do whatever it wishes to,
An OS has nothing to do with the description of the
flush operation of a C++ stream.
like waiting for another operation to terminate before
completing writing the data.
The device to which a stream is attached might be a 'real'
hardware device, or it might be one 'simulated' by an
OS (e.g. 'virtual device' such as a 'RAM-disk'). The
behavior of such devices and control of such by an OS
is outside of a discussion about C++ streams.
Using implementation-specific functions will give you
more control on the way the data reaches
destination. That's all I meant.
Well, yes, platform-specific features can give 'finer-grained'
control over a system, but that's nothing to do with C++
streams, which are an *abstraction* of external data.
I mean that in many situations, the 'automatic' flushing
of an output stream will already occur, as in my 'cout'
followed by 'cin' example. Also it's often good enough
to simply let flushing happen automatically when an output
stream gets destroyed. Of course there can be exceptions
to this, depending upon the problem domain. "Usually"
can mean many things to various people. It usually
means "common to a person's experiences and observations."
That is what I talked about,
Which part of the paragraph I wrote after that code
are you talking about? Is your answer to the question
yes or no? Were you talking about the 'IDE interference'?
If so, C++ stream flushing has nothing to do with that.
sorry, I was probably not
explicit enough.
Effective communication can often be difficult.
Probably should have said "not necessarily true".
... that if std::cout is attached to a video display,
the data displayed after a flush might 'disappear'.
Nothing about the C++ streams will cause (or prevent) this
behavior. The data will be sent to the device (or if you
prefer, to the OS's interface for it). What happens after
that is outside the language.
That is what I meant, yes.
But you were talking about this happening at program
termination. I meant 'midstream' during execution
where data is being output.
I gave it as a common example. It's very common to
have code such as:
std::cout << "Your name: ";
std::cin >> name;
There's no need to flush cout to ensure the prompt
appears before the input request occurs. This is
done automatically by default, via 'tie()'.
Contrast C's stdin and stdout, where a prompt
using 'printf()' needs a subsequent flush
before an input request e.g. with 'scanf()',
to ensure the prompt appears first.
To quote myself :
(just as it may flush it with any character).
I suppose I could have misunderstood your meaning.
No, there is nothing preventing the stream buffer
being flushed at whatever point the library implementor
(or a coder using a custom stream buffer) determines is
appropriate (this could be upon insertion of a character
with a certain value such as '\n' or some other value,
but there's no requirement for it).
One condition under which a flush with *always* happen
is when a character (with *any* value) is inserted,
but the stream buffer is already full. The stream
buffer must first be flushed in order to make room
for the new character.
It just seemed to me that you felt you were pointing
out something not obvious to most C++ programmers.
That post was not intended to teach you things you know, it
was primarly for the op.
And anyone else reading. I try to intercept incorrect
or unclear information being imparted here. It also
gives others a chance to point out when I err myself.
But thanks for the corrections.
You're welcome. Please be assured that my motives
are educational, not attempts to appear 'smarter'
than you or anyone else. You should have seen the
confusion I sometimes suffered when learning C, so
many years ago.
Hope you've found my comments helpful.
-Mike