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: