Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No const overloads for getting the underlying device of boost::iostream::stream #134

Open
LimpSquid opened this issue Jun 19, 2021 · 0 comments

Comments

@LimpSquid
Copy link

LimpSquid commented Jun 19, 2021

There seem to be no const overloads for getting the underlying Device of boost::iostream::stream. It might not be as trivial as by adding the following two lines, because sub-sequential calls do not seem to implement the const overloads either.

Device const & operator*() const { return *this->member; }
Device const * operator->() const { return &*this->member; }

I'm not sure if there is some reason why there are no const overloads for these particular methods. As far as I can see there shouldn't be any issue to add them. Currently I can work around this problem by marking the stream in the example as mutable.

In any case here an example: https://coliru.stacked-crooked.com/a/fe70537e096a2b0b.
Or use the code below:

#include <iostream>
#include <boost/iostreams/categories.hpp>
#include <boost/iostreams/stream.hpp>

struct stream_sink
{
    using char_type = char;
    using category = boost::iostreams::sink_tag;
    
    stream_sink(std::ostream & os) :
        stream(os)
    {}
    
    bool fail() const { return stream.fail(); }
    
    std::streamsize write(const char * s, std::streamsize n) 
    {
        stream.write(s, n);
        return n;
    }    
    
    std::ostream & stream;
};

struct cout_writer
{
    using stream_type = boost::iostreams::stream<stream_sink>;
    
    stream_type stream{std::cout};
    
    operator bool() { return !stream->fail(); }
    
    // Does not compile as there are no const overloads like below:
    // - Device const & operator*() const { ... }
    // - Device const * operator->() const { ... }
    operator bool() const { return !stream->fail(); }
    
    cout_writer & operator<<(std::string_view s)
    {
        stream << s;
        return *this;
    }
};

int main()
{
    cout_writer writer;
    
    writer << "foo\n";
}
@LimpSquid LimpSquid changed the title No const overloads for getting underlying device of boost::iostream::stream No const overloads for getting the underlying device of boost::iostream::stream Jun 19, 2021
rdoeffinger added a commit to rdoeffinger/iostreams that referenced this issue Feb 1, 2022
Also for the "component" and "obj" functions these use.
Fixes issue boostorg#134.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant