在C++中,很容易编写一个Guard类,它引用一个变量(通常是bool),当实例对象退出范围并被析构时,析构函数将变量重置为原始值。voidsomeFunction(){if(!reentryGuard){BoolGuard(&reentryGuardA,true);//dosomestuffthatmightcausereentryofthisfunction//thissectionisbothearly-exitandexceptionproof,withregardstorestoring//theguardvariabletoitsoriginalstate}}我正在寻找一种