Just for what it's worth (not much, IMO) if you make the a
in question the proper type, it's possible to get this to compile and have defined behavior. The trick (and in this case it's a dirty, evil trick) is to overload the post-fix increment operator to return an lvalue:
class dirty_evil_trick {
int val;
public:
dirty_evil_trick() : val(0) {}
// to demonstrate, I'll just make postfix increment act like prefix increment.
dirty_evil_trick operator++(int) {
++val;
return *this;
}
dirty_evil_trick operator++() {
++val;
return *this;
}
operator int(){ return val; }
};
As implied by the name, this is (at least in my opinion) truly horrible code. We've made both the prefix and postfix increment operators act like prefix increment. So, for example, if we exercise it as follows:
int main(){
dirty_evil_trick a;
std::cout << ++a++;
}
...it should print out 2
. And, since invoking the operator overload functions imposes ordering constraints, we should be able to count on that result with all conforming compilers.
I'll repeat once more though: this is not intended as advice about how you should write code. It's really just a demonstration of what the error message you got meant, and how you can write code that does let this actually work (or, at least compile and do something. Whether that qualifies as "working" is a whole 'nother question).