毫无疑问,很多人都熟悉Alexandrescus ScopeGuard先生模板(现在是Loki的一部分)和新版ScopeGuard11:http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012- 安德烈- Alexandrescu的系统性,错误处理-在-C
来源:https: //gist.github.com/KindDragon/4650442
在他在c ++和2012年以后的演讲中,他提到他无法找到一种方法来正确检测范围是否因异常而退出.因此,当且仅当由于异常而退出作用域时,他才能实现SCOPE_FAIL宏,该宏将执行提供的lambda(通常用于回滚代码).这将使得dismiss()成员函数不再需要,并使代码更具可读性.
既然我不像Alexandrescu先生那样天才或经验丰富,我希望实施SCOPE_FAIL并不像以下那么容易:
~ScopeGuard11(){ //destructor
if(std::uncaught_exception()){ //if we are exiting because of an exception
f_(); //execute the functor
}
//otherwise do nothing
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么不呢?