Will the following give a compilation error?
delete cout;
delete cin;
The answer is : No.
It is a flaw in the implementation of stream classes from the Standard library. They have the following conversion function to void*
type, which means, all stream objects can be implicitly converted to void*
:
operator void * ( ) const;
This is very useful in general as it lets us write very idiomatic loop, say, when reading input from files. But at the same time, it lets user to write delete stream
. As I said, you can delete any stream object. So all of these are allowed:
delete ss; //declare std::stringstream ss;
delete iss; //declare std::istringstream iss;
delete oss; //declare std::ostringstream oss;
Only that they'll give a warning, saying (see at ideone):
warning: deleting ‘void*’ is undefined
which you can easily avoid just by casting, say, tochar*
. But the program has still issue, and most likely will crash when running it.
--
So my question is, has this issue been addressed and fixed, in C++11? The following article provides one fix for this problem:
--
Edit:
From @Xeo's comment on @Alf's answer:
The paper which proposed a fix for this issue:
It has apparently been fixed.
At least, in N3290 you have
std::basic_ios::operator bool
instead of thatvoid*
conversion, and thisoperator bool
is declaredexplicit
.Note that C++98/C++03 did not support
explicit
type conversion operators, but C++11 does.An
explicit
type conversion operatorAnd that might seem to be impractical for the condition in e.g. a
while
orfor
statement.Happily,
where
bool t(e);
is a direct-initialization.E.g. you don’t have to explicit convert a stream object used as condition in a
while
, because there is implicitly an explicit conversion (he he).Unfortunately, searching N3290 I can’t find any list of the “certain language constructs” where this happens, but in comments to this answer JohannesD wrote:
Cheers & hth.,
If I can give my 2 cents, I think the standard library "flawed" a bit, with all the good intentions.
The
operator void*()
had been introduced to allow code likewhile(stream)
orif(!stream)
orwhile(stream && ...)
, without giving an implicit access to integer arithmetic (thatoperator bool
whould have given). In fact, this disable integer arithmetic, but gives access to pointer features (like delete ...).Now, in C++0x, an
explicit oeprator bool()
had been introduced. It doesn't implicitly give access to whatever feature, since it requires an implicit conversion. But ... wait a bit: 'while(bool(stream))' or evenwhile(static_cast<bool>(stream))
are so wordy... Operator ! is explicit, and 'while(!!stream)' looks so effective that I even wonder why not accept this as a paradigm:If I want something to be explicitly converted into bool, I just provide an
operator!()
and give to!
the memaning of "is not valid" and of!!
as "is valid".Much safer then an implicit conversion and not uselessly wordy: after all
!
exist from ever!