我正在开发一个记录器,它有一条消息继承 std::stringstream
.
当我想打印消息时,我调用一个函数来接受消息 const
:
void logger::log_message(message const & msg)
在该函数中,我想检查消息中是否写入了任何内容。如果没有,我可以忽略这个电话。我使用以下内容:
if(const_cast<message &>(msg).tellp() == 0)
{
return;
}
我必须使用 const_cast<>()
因为tellp()
不是const
。我想知道为什么检索当前的查找位置会对流产生副作用。我能理解seekp()
这会改变位置,但不会改变 tellp()
。这可能是规范中的错误吗?
最佳答案
如果失败,tellp()
将更改对象的 rdstate
。
参见: https://en.cppreference.com/w/cpp/named_req/UnformattedOutputFunction
关于c++ - 为什么tellp() 是一个非常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57237779/