Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_ios<CharT,Traits>::operator bool

      From cppreference.com
      <cpp‎ |io‎ |basic ios
       
       
       
       
      operator/* unspecified-boolean-type */()const;
      (1)(until C++11)
      explicit operatorbool()const;
      (2)(since C++11)

      Checks whether the stream has no errors.

      1) Returns a value that evaluates tofalse in a boolean context iffail() returnstrue, otherwise returns a value that evaluates totrue in a boolean context.
      2) Returnstrue if the stream has no errors and is ready for I/O operations. Specifically, returns!fail().

      This operator makes it possible to use streams and functions that return references to streams as loop conditions, resulting in the idiomatic C++ input loops such aswhile(stream>> value){...} orwhile(std::getline(stream, string)){...}. Such loops execute the loop's body only if the input operation succeeded.

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      1) A value that evaluates totrue in a boolean context if the stream has no errors, a value that evaluates tofalse in a boolean context otherwise.
      2)true if the stream has no errors,false otherwise.

      [edit]Notes

      This conversion can be used in contexts where abool is expected (e.g. anif condition). However,implicit conversions (e.g. toint) that can occur withbool are not allowed.

      In C++98,operatorbool could not be provided directly due tothe safe bool problem. The initial solution in C++98 is to provideoperatorvoid*, which returns a null pointer iffail() returnstrue or a non-null pointer otherwise. It is replaced by the resolution ofLWG issue 468, which allowsSafe Bool idiom to be applied.

      Since C++11, conversion functions can beexplicit. The resolution ofLWG issue 1094 introduced the explicitoperatorbool and the boolean conversion is now safe.

      [edit]Example

      Run this code
      #include <iostream>#include <sstream> int main(){std::istringstream s("1 2 3 error");int n; std::cout<<std::boolalpha<<"s is "<<static_cast<bool>(s)<<'\n';while(s>> n)std::cout<< n<<'\n';std::cout<<"s is "<<static_cast<bool>(s)<<'\n';}

      Output:

      s is true123s is false

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 468C++98operatorvoid* was provideda conversion function to an unspecified boolean type is provided instead

      [edit]See also

      The following table shows the value ofbasic_ios accessors (good(),fail(), etc.) for all possible combinations ofios_base::iostate flags:

      ios_base::iostate flagsbasic_ios accessors
      eofbitfailbitbadbitgood()fail()bad()eof()operator booloperator!
      false false falsetrue false false falsetrue false
      false falsetrue falsetruetrue false falsetrue
      falsetrue false falsetrue false false falsetrue
      falsetruetrue falsetruetrue false falsetrue
      true false false false false falsetruetrue false
      true falsetrue falsetruetruetrue falsetrue
      truetrue false falsetrue falsetrue falsetrue
      truetruetrue falsetruetruetrue falsetrue
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_ios/operator_bool&oldid=148329"

      [8]ページ先頭

      ©2009-2025 Movatter.jp