Stream state question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • john

    #1

    Stream state question

    In TC++PL 3, on page 616, "class basic_ios" has the public members

    "operator void *() const; // nonzero if !fail()
    bool operator!() const { return fail(); }"

    for using them in conditional statements in the style:

    if(cin) // if( !void *(0) )

    if(!cin) // if( cin.operator!()


    Why isn't operator bool() used instead? For historical reasons?
  • James Kanze

    #2
    Re: Stream state question

    On Sep 30, 12:28 am, john <j...@no.spamwr ote:
    In TC++PL 3, on page 616, "class basic_ios" has the public members
    "operator void *() const; // nonzero if !fail()
    bool operator!() const { return fail(); }"
    for using them in conditional statements in the style:
    if(cin) // if( !void *(0) )
    if(!cin) // if( cin.operator!()
    Why isn't operator bool() used instead? For historical
    reasons?
    The most obvious reason is that iostream was part of the
    language before bool was. And that bool is an integral type,
    which can lead to unexpected results when used with the >and
    << operators.

    --
    James Kanze (GABI Software) email:james.kan ze@gmail.com
    Conseils en informatique orientée objet/
    Beratung in objektorientier ter Datenverarbeitu ng
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

    Comment

    • john

      #3
      Re: Stream state question

      James Kanze wrote:
      >
      >Why isn't operator bool() used instead? For historical
      >reasons?
      >
      The most obvious reason is that iostream was part of the
      language before bool was. And that bool is an integral type,
      which can lead to unexpected results when used with the >and
      << operators.

      basic_osstream has explicit "operator<< ()" definitions for both "bool"
      and "const void *".

      Comment

      Working...