Why is `is_open()` non-const?

Viewed 699

I have a function similar to below which is const and needs to check that a file stream is open prior to continuing:

bool MyClass::checkSomeStuff() const
{
    // Where outputFile_ is a std::ofstream
    if ( ! outputFile_.is_open() )
    {
        throw std::runtime_error( "Output file not open." );
    }

    // ... do more stuff

However, It seems I can't do this as is_open() is declared as:

bool is_open ( );

(i.e. non-const)

To me it seems a bit odd that a function like this - which is clearly a pure accessor - should be non-const. Is there a logic behind it which makes sense?

3 Answers
Related