c++constantsstringstreamseek

Why would tellp() be a non-const?


I am working on a logger which has a message inheriting std::stringstream.

At the point when I want to print out the message, I call a function which accepts the message as a const:

void logger::log_message(message const & msg)

In that function, I want to check whether anything was written to the message. If not, I can just ignore the call. I use the following:

if(const_cast<message &>(msg).tellp() == 0)
{
    return;
}

I had to use the const_cast<>() because tellp() is not const. What I'm wondering is why would retrieving the current seek position have side effects on the stream. I could understand the seekp() which changes the position, but not the tellp(). Could that be a mistake in the specs?


Solution

  • The tellp() will change the rdstate of the object if it fails.

    See: https://en.cppreference.com/w/cpp/named_req/UnformattedOutputFunction