fis*_*000 4 c++ exception raii try-catch noexcept
好吧,如果我使用RAII惯用语来管理某些上下文属性*,如果我在try块的开头裸体使用它,它是否会像我期望的那样工作?
换句话说,如果我有这个:
struct raii {
raii() {
std::cout << "Scope init"
<< std::endl; }
~raii() {
std::cout << "Scope exit"
<< std::endl; }
};
Run Code Online (Sandbox Code Playgroud)
......我成功地使用它:
{
raii do_the_raii_thing;
stuff_expecting_raii_context();
/* … */
}
Run Code Online (Sandbox Code Playgroud)
...如果我这样做,RAII实例将以相同的方式工作:
try {
raii do_the_raii_thing;
stuff_expecting_raii_context_that_might_throw();
/* … */
} catch (std::exception const&) {
/* … */
}
Run Code Online (Sandbox Code Playgroud)
这可能是一个愚蠢的问题,但我想在此检查自己的理智 - 我对noexcept保证的微妙之处以及其他与例外相关的细节感到模糊- 所以请原谅我的天真
[*]对于那些好奇的人来说,这是我在RAII管理的Python C-API狡猾的GIL(全球翻译锁),在我的具体案例中