Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete cout; delete cin; do not give compilation error - a flaw in the Standard library?

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:

  • The Safe Bool Idiom

--

Edit:

From @Xeo's comment on @Alf's answer:

  • Is the safe-bool idiom obsolete in C++11?

The paper which proposed a fix for this issue:

like image 574
Nawaz Avatar asked Sep 17 '11 08:09

Nawaz


1 Answers

It has apparently been fixed.

At least, in N3290 you have std::basic_ios::operator bool instead of that void* conversion, and this operator bool is declared explicit.

Note that C++98/C++03 did not support explicit type conversion operators, but C++11 does.

An explicit type conversion operator

N3290 §12.3.2/2;
is only considered as a user-defined conversion for direct-initialization (8.5)

And that might seem to be impractical for the condition in e.g. a while or for statement.

Happily,

N3290 §4/3;
An expression e can be implicitly converted to a type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t (8.5). Certain language constructs require that an expression be converted to a Boolean value. An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t (8.5). The effect of either implicit conversion is the same as performing the declaration and initialization and then using the temporary variable as the result of the conversion.

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:

Searched through the FDIS for “contextually”, and the whole list seems to be: if, while, do, for, noexcept, and static_assert conditions; the first operand of ?:; both operands of && and ||; and the operand of !.

Cheers & hth.,

like image 197
Cheers and hth. - Alf Avatar answered Oct 14 '22 10:10

Cheers and hth. - Alf