#include<iostream>
#include<csetjmp>
using namespace std;
class Rainbow{
public:
Rainbow(){cout<<"Rainbow()"<<endl;}
~Rainbow(){cout<<"~Rainbow()"<<endl;}
};
jmp_buf kansas;
void oz()
{
Rainbow rb;
for(int i=0;i<3;i++)
cout<<"There's no palce like home"<<endl;
longjmp(kansas,47);
}
int main()
{
if(setjmp(kansas)==0)
{
cout<<"tornado,witch munchkins^^"<<endl;
oz();
}
else
{
cout<<"auntie em!"<<"I had the strangest dream^^^^^"<<endl;
}
}
其中由于longjmp跳转到了setjmp,所以Rainbow对象没有被销毁。
但在某些C++编译器中,包含了扩展的longjmp(),能够清除栈中的对象,但这种行为是不可移植的