Standard tellp behavior on empty ostringstream

I have a question about standard call behavior tellpon empty ostringstream. I have a foo function that calls the tellpfirst:

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

int main()
{
    std::ostringstream os;
    foo(os);
}

In Visual Studio 2005, calling this function with the newly created and empty ostringstreamcauses the variable posto be set to invalid pos_type, which in Visual Studio 2005 is set to pos_type(_BADOFF).

ofstreamdoesn't have the same behavior where tellpreturns pos_type(0), which is valid pos_type.

Is this a standard match? Is this consistent with other compilers?

+3
source share
1 answer

27.6.2.4:

pos_type tellp();

: fail()!= false, pos_type (-1) . rdbuf() → pubseekoff (0, cur, out).

pubseekoff -1 . , ostringstream, , , undefined . , , ostringstream 0, ostream -1 ostream 0.

+1

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


All Articles