views:

350

answers:

1

I have a question on the standard behavior of calling tellp on an empty ostringstream. I have a function foo which calls tellp the first thing:

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 a newly created and empty ostringstream results in the pos variable to be set to an invalid pos_type, which in Visual Studio 2005 is set to pos_type(_BADOFF).

ofstream does not have the same behavior, where tellp returns pos_type(0), which is a valid pos_type.

Is this standard conforming behavior? Is this behavior consistent with other compilers?

+1  A: 

27.6.2.4:

pos_type tellp();

Returns: if fail() != false, returns pos_type(-1) to indicate failure. Otherwise, returns rdbuf()->pubseekoff(0, cur, out).

And pubseekoff returns -1 on fail. But am not sure why this happens for you in the case of ostringstream, maybe was too tired to find the words about undefined or implementation-dependent. Using my common sense I would say that for ostringstream this should give 0, for default constructed ostream -1, and for ostream with freshly opened file 0.

Anonymous
I agree that this should give 0 for `ostringstream`, so I guess this is a bug in VS2005's C++ Standard Library.
dalle