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;
}
}
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;
}
};
Iterator & operator++() {
if(m_rangePtr) {
++m_currVal;
}
return *this;
}
Iterator operator++(int) {
Iterator it(*this);
operator++();
return it;
}
bool operator==(const Iterator &rhs) {
if(m_rangePtr) {
if(m_rangePtr == rhs.m_rangePtr ) {
return m_currVal == rhs.m_currVal;
}
}
return false;
}
bool operator!=(const Iterator &rhs) {
return !(*this==rhs);
}
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(this->first > this->second) {
this->first = 0;
this->second = 0;
}
}
iterator begin() {
return iterator(*this);
};
iterator end() {
return iterator(*this,true);
};
};
};
MWE:
!