14

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:

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 7
    c++ provides you lots of ways to shoot yourself in the foot. This is one of them ;) – BЈовић Sep 17 '11 at 08:43
  • 1
    I would say that this is shooting straight between the eyes! :-) – TCS Sep 17 '11 at 08:46
  • @VJo: Yes, if it's incorrectly implemented. If you implement Safe Bool Idiom, then you can't shoot yourself in the foot that way. – Nawaz Sep 17 '11 at 09:09
  • 8
    Luckily there is a simple workaround available in the language of "Well don't do that then" :) – jcoder Sep 17 '11 at 09:18
  • 3
    @Nawaz: the Safe Bool idiom was invented after C++98 was published (I don't know for C++03) so it's only normal. Note though that they had already thought about it, somewhat, since they used `void*` (which warns in most contexts) rather than `char*` directly or `bool`. – Matthieu M. Sep 17 '11 at 09:20
  • 1
    http://old.nabble.com/-v3---PATCH--Use-explicit-operator-bool-for-std%3A%3Abasic_ios-in-C%2B%2B0x-mode.-td32257515.html – Johannes Schaub - litb Sep 17 '11 at 09:57
  • 2
    Shouldn't you be more worried about [`memcpy(cout, cin, 10);`](http://ideone.com/UTSVO) No warnings whatsoever. – Ben Voigt Sep 17 '11 at 18:53
  • @Ben: Haha.. that's even better. – Nawaz Sep 17 '11 at 18:54
  • 1
    @Nawaz: really nice question. I didn't know about this. – Destructor Aug 06 '15 at 18:20
  • 1
    Well there was one thing I forgot to address, namely, whether a diagnostic is required for `delete p` where `p` is a `void*`. I don't know. But now I have some cooking to attend to, so... – Cheers and hth. - Alf Aug 07 '15 at 04:47
  • @BenVoigt: your link about `memcpy(cout,cin,10)` seems to be broken ? Can you upload the working link again ? – Destructor Nov 11 '16 at 15:44
  • 1
    @Destructor: Please complain to the ideone.com team -- they're violating their own public promises about link lifetime but aren't likely to do anything about it unless they get a large volume of requests about it. – Ben Voigt Nov 11 '16 at 15:52
  • but is `memcpy(cout,cin,10)` undefined behaviour ? What standard says about this ? – Destructor Nov 11 '16 at 16:30
  • @Destructor: Yes, it is UB. What do *you* expect it to do, BTW? – Nawaz Nov 11 '16 at 17:38

2 Answers2

18

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.,

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • So with the `explicit` conversion, can we use it in the `while` loop as we do with C++03 streams? – Nawaz Sep 17 '11 at 09:11
  • 5
    Yes, that's the silly C++11 "implicitly explicit". He he. :-) – Cheers and hth. - Alf Sep 17 '11 at 09:14
  • 1
    @Alf: would you mind expanding your explanation of explicit operators then. I thought that it would require qualification, but apparently it does not... so when can "implicit" conversion take place using the `explicit` operator :x ? – Matthieu M. Sep 17 '11 at 09:21
  • @Alf: I agree with Matthieu. Please expand your explanation of explicit operators. – Nawaz Sep 17 '11 at 09:32
  • 4
    And it has been fixed at the core language side too. The `delete` operator now only considers conversion functions to pointer-to-object types. So instead of such a case having undefined behavior, it now requires a diagnostic. Of course that doesn't require a change on his implementation formally (he already got a diagnostic), but most probably the implementation will now reject instead of merely warn... – Johannes Schaub - litb Sep 17 '11 at 09:53
  • 3
    This is part of the Standard conversions i clause 4: "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`." Obviously Alf finds it amusing that a "contextual conversion" uses an explicit operator to perform what used to be an implicit conversion. – Bo Persson Sep 17 '11 at 09:54
  • 3
    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 `!`. – JohannesD Sep 17 '11 at 12:40
  • 2
    Related: [Is the safe-bool idiom obsolete?](http://stackoverflow.com/questions/6242768/is-the-safe-bool-idiom-obsolete). – Xeo Sep 18 '11 at 00:21
  • @Alf I think you confused me and JohannesD :) – Johannes Schaub - litb Sep 25 '11 at 00:05
0

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 like while(stream) or if(!stream) or while(stream && ...), without giving an implicit access to integer arithmetic (that operator 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 even while(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!

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
  • 1
    You don't have to write `while(!!stream)`, because the `explicit` conversion is invoked implicitly for `while` and other constructs where a `bool` value is expected. See my answer. Cheers & hth., – Cheers and hth. - Alf Sep 17 '11 at 18:49
  • You might want to look up the "safe bool idiom." (Or don't, since it's obsolete with C++11.) I tried going with `!!` for a little while and decided the ugliness wasn't worth the marginal safety… – Potatoswatter Sep 17 '11 at 21:14
  • @Alf: yes, with new compilers doesn't matter aymore, but with old ... I saw so many tricks just to avoid to type a ! .... Anyway, thanks. – Emilio Garavaglia Sep 18 '11 at 17:26
  • @Potatoswatter: It's a matter of taste... there are many other ugly things in C++ ... – Emilio Garavaglia Sep 18 '11 at 17:27