tellp 在空 ostringstream 上的标准行为

dal*_*lle 5 c++ standards-compliance stringstream

tellp我对调用空的标准行为有疑问ostringstream。我有一个函数 foo 调用tellp第一件事:

void foo(std::ostream& os)
{
    std::ostream::pos_type pos = os.tellp();
    // do some stuff.
}

int main()
{
    std::ostringstream os;
    foo(os);
}
Run Code Online (Sandbox Code Playgroud)

在 Visual Studio 2005 中,使用新创建的空变量调用此函数会导致ostringstream变量pos设置为 invalid pos_type,而在 Visual Studio 2005 中该变量设置为pos_type(_BADOFF)

ofstream没有相同的行为,其中tellpreturns pos_type(0),这是一个有效的pos_type.

这个行为符合标准吗?此行为与其他编译器一致吗?

Ano*_*ous 1

27.6.2.4:

pos_type tellp();
Run Code Online (Sandbox Code Playgroud)

返回:如果fail() != false,则返回pos_type(-1)以指示失败。否则,返回 rdbuf()->pubseekoff(0, cur , out )。

pubseekoff在失败返回 -1。但我不确定为什么在ostringstream的情况下会发生这种情况,也许太累了而找不到有关undefinedimplementation dependent的单词。根据我的常识,我会说对于ostringstream应该给出 0,对于默认构造的 ostream -1,对于新打开文件的 ostream 应该给出 0。