Skip to content

<streambuf>: std::basic_streambuf::seekpos() and std::basic_streambuf::seekoff() doesn't work with custom type.  #2120

@fsb4000

Description

@fsb4000

Describe the bug
In the implementation of std::basic_streambuf::seekpos(), the return statement constructs a value of type std::streampos(-1). This is only works correctly when pos_type is std::streampos. When pos_type is something else, such as std::fpos with a custom state type, the return statement generates a warning because the constructed value gets converted to pos_type via two user defined conversions (which is not allowed by standard C++). The resulting code does the right thing, but the warnings are annoying, and the implementation is clearly incorrect.

As far as I can tell, both return streamoff(-1) and return pos_type(-1) are proper ways to correct the return statement.

Another member function, std::basic_streambuf::seekoff(), suffers from the same problem.

STL/stl/inc/streambuf

Lines 363 to 366 in a9321cf

virtual pos_type __CLR_OR_THIS_CALL seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out) {
// change to specified position, according to mode
return streampos(-1);
}

STL/stl/inc/streambuf

Lines 357 to 361 in a9321cf

virtual pos_type __CLR_OR_THIS_CALL seekoff(
off_type, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out) {
// change position by offset, according to way and mode
return streampos(-1);
}

Command-line test case

#include <string>
#include <streambuf>

struct State {};

struct CharTraits : std::char_traits<char> {
    using state_type = State;
    using pos_type = std::fpos<State>;
};

struct Streambuf : std::basic_streambuf<char, CharTraits> {};

https://gcc.godbolt.org/z/5b7Yfsefo

Expected behavior
No warnings should be generated.

STL version
git commit hash
https://github.com/microsoft/STL/commit/a9321cfe53ea31a7e197d5d8336167d6ca3de8b6

Additional context
DevCom-1495852

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions