Weird Infinite Loop (gcc 4.7.3, compiler error?)

I have the following code:

Where am I stupidly sorting through a container (self-recording). If I compile this function with cout, it will work and the program will exit correctly after iteration! Note: cout does not interfere with a makeshift container! If I then comment on this , the test will begin to end indefinitely and will not end! My eyeballs that almost fall into my head! What's going on here? My first assumption was that the compiler is doing something wrong? (but why?), or maybe the branch prediction incorrectly changes me and somehow iterator doubles? (which does not lead to an end iterator ()).

I really can not explain what is happening?

    int loops = 100;
    int n = 0;

        RangeTypes<Type>::ContainerType range(a);
        INIT_TIMER
        START_TIMER
        for(int i=1; i<loops; i++) {
            for(auto it=range.begin(); it != range.end(); ++it) {
                n +=  *it;
                //std::cout << i << std::endl;
            }
        }
        STOP_TIMER("Range: ")

RangeType , , !

template<typename Type>
struct RangeTypes {
    using  RangeType = std::pair<Type,Type> ;

    class Iterator {
    public:

        Iterator(): m_rangePtr(nullptr), m_currVal(0) {};
        ~Iterator() {};
        Iterator(RangeType & range, bool atEnd=false): m_rangePtr(&range) {
            if(atEnd) {
                m_currVal = m_rangePtr->second;
            } else {
                m_currVal = m_rangePtr->first;
            }
        };

        /** pre-increment ++it
        * Allow to iterate over the end of the sequence
        */
        Iterator & operator++() {
            if(m_rangePtr) {
                ++m_currVal;
            }
            return *this;
        }

        /** post-increment it++ */
        Iterator operator++(int) {
            Iterator it(*this);
            operator++();
            return it;
        }

        bool operator==(const Iterator &rhs) {
            if(m_rangePtr) {
                if(m_rangePtr == rhs.m_rangePtr ) { // if sequences are the same
                    return m_currVal == rhs.m_currVal;
                }
            }
            return false;
        }

        // Return false if the same!
        bool operator!=(const Iterator &rhs) {
            return !(*this==rhs);
        }

        // get current value;
        Type operator*() {
            return m_currVal;
        }

    private:
        RangeType * m_rangePtr;
        Type m_currVal;
    };

    class ContainerType : public RangeType {
    public:

        typedef Iterator iterator;

        ContainerType(RangeType & range ): RangeType(range) {
            // if range wrong, set to no range!
            if(this->first > this->second) {
                this->first = 0;
                this->second = 0;
            }
        }
        iterator begin() {
            return iterator(*this);
        };
        iterator end() {
            return iterator(*this,true);
        };
    };
};

MWE:

!

+4

Source: https://habr.com/ru/post/1543254/


All Articles