Suppose I have a C ++ class in RAII style:
class StateSaver { public: StateSaver(int i) { saveState(); } ~StateSaver() { restoreState(); } };
... for use in my code:
void Manipulate() { StateSaver save(1);
... The goal is to enter into some state, do something, and then leave this state when I leave this area. Is there a way to prevent this typo from compiling (or warning, or complaining somehow so that I can spot an error)?
void Manipulate() { StateSaver(1); // ruh-roh, state saved and immediately restored! // ...do stuff that modifies state }
I don't know anything about C ++ itself, which I could use to prevent this, but that does not mean that it does not exist. If there was nothing in C ++, extensions specific to the compiler are permissible. What interests me most is everything related to gcc and msvc (once icc, ideas for other compilers are welcome, but less likely to be useful), so hacking for any of them would be useful (abstracted into the corresponding macro definitions # ifdef'd, of course )
c ++ gcc visual-c ++ icc raii
Jeff walden
source share