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

后端 未结 2 1511
没有蜡笔的小新
没有蜡笔的小新 2021-02-13 11:52

Will the following give a compilation error?

delete cout;
delete cin;

The answer is : No.

It is a flaw in the implementation of strea

相关标签:
2条回答
  • 2021-02-13 12:02

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

    0 讨论(0)
  • 2021-02-13 12:13

    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!

    0 讨论(0)
提交回复
热议问题