Why does std::basic_ios overload the unary logical negation operator?

前端 未结 3 1844
耶瑟儿~
耶瑟儿~ 2020-12-17 21:02

The C++ IO streams\' base class std::basic_ios defines operator void*() to return !fail() and operator!() to return

3条回答
  •  一整个雨季
    2020-12-17 21:34

    Looking at the implementation of MinGW, which is shipped with Codeblocks shows me this code:

      operator void*() const
      { return this->fail() ? 0 : const_cast(this); }
    
      bool
      operator!() const
      { return this->fail(); }
    

    It seems to me, that the operator void*() const is intended for more uses than only for the check of success. On top of that it serves also as a casting operator (we're returning this). Right now I am scratching my head a little bit, why we may want to cast this to void*. But the rest is quite clear - if you've got an erroneous stream anyway, you can also return null.

提交回复
热议问题