c++istream-iterator

Why I cannot create a stream iterator?


I am supposed to create a looks-like stream iterator class so that I can read from the input stream when incrmenting an object of my class.

I've done this:

template<class T>
struct istrm_it{
    istrm_it() = default;
    istrm_it(std::istream& in) :
        in_(in), val_(T()) {
        in_ >> val_;
    }
    T val_;
    std::istream& in_ = std::cin;
    istrm_it& operator++() { in >> val_; return *this; };
    istrm_it& operator++(int) { in_ >> val_; return *this; };
    T operator*() { return val_; }
    bool operator==(const istrm_it& rhs)const { return in_ == rhs.in_; }
    bool operator!=(const istrm_it& rhs) const{ return in_ != rhs.in_; }
};

int main(){

    istrm_it<int> it(std::cin), e;
    cout << *it << endl; // ok

    it++; // ok read the next value from the nput stream

    vector<int> vi;
    while (it != e) // the problem here
        vi.push_back(*it++);

    for (auto i : vi)
        std::cout << i << ", ";
    std::cout << std::endl;


    std::cout << std::endl;
}

What I get: Severity Code Description Project File Line Suppression State Error C2678 binary '!=': no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)


Solution

  • The problem is in the comparison in_ != rhs.in_ (and in_ == rhs.in_ as well). You can't compare streams.

    Instead you need to keep some kind of "is end iterator" or "is at end-of-file" state that is set when you reach end-of-file, and which is set by default in the default-constructed iterator object.