// Demonstration of difference between ANSI and Cfront rules // for lifetime of compiler-generated temporary. // Program prints "Cfront" when compiled with Cfront rules, // and "ANSI" when compiled by ANSI rules. #include enum State {UNKNOWN,LIVE,DEAD}; class Object { private: State* my_state; public: Object( State* state ) : my_state(state) {*my_state=LIVE;} ~Object() {*my_state=DEAD;} }; void f( const Object& c ) {} main() { State state = UNKNOWN; // Compiler must generate temporary Object to pass as actual paramter to f. // The state of the object is tracked in state. f( &state ); // Under ANSI rules, the temporary is destroyed after f(&state) // is evaluated, and thus is dead now. switch( state ) { case LIVE: cout << "Cfront\n"; break; case DEAD: cout << "ANSI\n"; break; default: cout << "broken compiler\n"; break; } // Under Cfront rules, the temporary survives until the end of the // enclosing block. Thus with Cfront, it is live up until here. }