Man würde erwarten, dass sie genau so funktionieren, aber.Ist C++ 11-Bereich für Funktionalität nicht gleich klassisch?
class Range
{
public:
class DontMindMe
{
public:
DontMindMe(int a) : a_(a) {}
bool operator ==(const DontMindMe& ot) {return a_ == ot.a_;}
bool operator !=(const DontMindMe& ot) {return !(*this==ot);}
int operator *(void) {return a_;}
DontMindMe& operator++() {++a_; return *this;}
DontMindMe operator++(int) {auto temp{*this}; ++a_; return temp;}
private:
int a_;
};
Range(int a, int b, const std::string& msg)
: a_(a), b_(b) {std::cout << msg << std::endl;}
DontMindMe begin() {return a_;}
DontMindMe end() {return b_;}
private:
int a_;
int b_;
};
int main()
{
for(auto it = Range::DontMindMe(1); it != Range(1,10, "C++").end(); ++it);
std::cout << std::endl;
for(auto it : Range(1,10, "C++11"));
}
Erste Schleife druckt die Nachricht 10 mal, zweite druckt sie nur einmal. Sind sie nicht gleich? Kann dieser Unterschied die alte Code-Überarbeitung erschweren?
Die Praxis, das Ende des Bereichs bei jeder Schleifeniteration neu zu berechnen, ist verpönt - und zu Recht. – SergeyA