jeudi 26 juillet 2018

C++11: Custom range-based loop without copy constructor

I would like to have a custom range-based loop but without a copy constructor. It seems there is a copy at some point but I do not understand why.

Here is the sample I would like to run: http://cpp.sh/63pa6

class Range
{
public:
    Range( int begin, int end )
        : iter_( begin ),
          last_( end )
    {
    }
    Range(Range&&) = default;
    Range& operator=(Range&&) = default;

    Range(const Range&) = delete;

    bool operator!=( const Range & /*unused*/ ) const
    {
        return iter_ != last_;
    }

    void operator++()
    {
        ++iter_;
    }

    const Range &begin() const
    {
        return *this;
    }

    const Range &end() const
    {
        return *this;
    }

    int operator*() const
    {
        return iter_;
    }
private:
    int iter_;
    int last_;
};

Aucun commentaire:

Enregistrer un commentaire