Can someone explain why the below loop in main()
does not terminate
#include <iostream>
enum day {
Sunday, // 0
Monday, // 1
Tuesday, // 2
Wednesday, // 3
Thursday, // 4
Friday, // 5
Saturday, // 6
not_a_day // 7
};
day operator++(day d) {
d = (day)(d + 1);
return d;
}
int main() {
day d;
d = Sunday;
while (d <= Saturday) {
++d;
}
std::cout << d << std::endl;
return 0;
}
When I change the overloaded operator to use references, it works as expected, returning 7
.
day &operator++(day &d) {
d = (day)(d + 1);
return d;
}
Example taken from here.
I also do not understand why the compiler is able to cast day
to an int
with the +
operator, but is not able to do so for ++
?