How would std :: ostringstream convert to bool?

I came across this code.

    std::ostringstream str;
    /// (some usage)
    assert( ! str );

What does it mean when used in context ? ostringstream bool

Perhaps this is a misuse that happens to compile and run?

+3
source share
3 answers

It tells you if the stream is really valid. This is what all threads can do. For example, a file stream may be invalid if the file was not opened properly.

( bool) explicit operator bool ++ 11 void* ++ 11.

, , . , .

: bool void* , .

+10

. :

istringstream is;
is.str( "foo" );
int x;
is >> x;

if ( ! is ) {
   cerr << "Conversion failed";
}

I'm not sure that any of the standard streaming functions can cause ostringstream to work poorly, but you can certainly write them yourself.

+1
source

Source: https://habr.com/ru/post/1707877/


All Articles