前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >C++核心准则E.19:如果无法选择适当的资源句柄,使用final_action表现清除处理​

C++核心准则E.19:如果无法选择适当的资源句柄,使用final_action表现清除处理​

作者头像
面向对象思考
发布于 2020-08-10 01:56:12
发布于 2020-08-10 01:56:12
55800
代码可运行
举报
运行总次数:0
代码可运行

E.19: Use a final_action object to express cleanup if no suitable resource handle is available

E.19:如果无法选择适当的资源句柄,使用final_action表现清除处理

Reason(原因)

finally is less verbose and harder to get wrong than try/catch.

和try/catch比起来,finally更加简练并不容易出错。

Example(示例)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
void f(int n)
{
    void* p = malloc(n);
    auto _ = finally([p] { free(p); });
    // ...
}
Note(注意)

finally is not as messy as try/catch, but it is still ad-hoc. Prefer proper resource management objects. Consider finally a last resort.

finally不像try/catch那样凌乱,然仍然是针对具体问题的特殊对策。使用适当的资源管理对象的方式更好。将finally视为最后一招。

Note(注意)

Use of finally is a systematic and reasonably clean alternative to the old goto exit; technique for dealing with cleanup where resource management is not systematic.

使用finally一种系统化、合理化的代替既有代码中goto exit的方式。使用这个技术可以处理资源没有被系统化管理的问题。

Enforcement(实施建议)

Heuristic: Detect goto exit;

启发式的:检出goto exit;

关于finally

finally是gsl提供的一个支持函数,可以生成一个用户释放资源的清除动作。具体实现请参考以下链接:https://github.com/microsoft/GSL/blob/master/include/gsl/gsl_util

原文链接 https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#e19-use-a-final_action-object-to-express-cleanup-if-no-suitable-resource-handle-is-available

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-08-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
C++核心准则​NR.6:不要将所有清理操作放在函数最后并使用goto语句跳转
goto is error-prone. This technique is a pre-exception technique for RAII-like resource and error handling.
面向对象思考
2020/11/10
5100
C++核心准则​NR.6:不要将所有清理操作放在函数最后并使用goto语句跳转
C++核心准则E.27:如果无法抛出异常,系统化运用错误处理代码
E.27: If you can't throw exceptions, use error codes systematically
面向对象思考
2020/08/13
5150
C++核心准则E.6:使用RAII防止资源泄露
Leaks are typically unacceptable. Manual resource release is error-prone. RAII ("Resource Acquisition Is Initialization") is the simplest, most systematic way of preventing leaks.
面向对象思考
2020/08/04
3390
C++核心准则E.18:最小限度显式使用try/catch
try/catch is verbose and non-trivial uses are error-prone. try/catch can be a sign of unsystematic and/or low-level resource management or error handling.
面向对象思考
2020/08/10
3620
C++核心准则E.2:通过抛出异常来表明函数无法执行指定的任务
To make error handling systematic, robust, and non-repetitive.
面向对象思考
2020/07/28
4540
C++核心准则C.31:类请求的所有资源必须在析构函数释放
Prevention of resource leaks, especially in error cases.
面向对象思考
2020/03/25
6010
C++核心准则R.1: 使用资源句柄自动管理资源并RAII(资源获取即初始化)
To avoid leaks and the complexity of manual resource management. C++'s language-enforced constructor/destructor symmetry mirrors the symmetry inherent in resource acquire/release function pairs such as fopen/fclose, lock/unlock, and new/delete. Whenever you deal with a resource that needs paired acquire/release function calls, encapsulate that resource in an object that enforces pairing for you -- acquire the resource in its constructor, and release it in its destructor.
面向对象思考
2020/03/25
7810
C++核心准则E.26:如果无法抛出异常,尽快进行失败处理​
E.26: If you can't throw exceptions, consider failing fast
面向对象思考
2020/08/13
3710
C++核心准则ES.76:避免使用goto语句​
Readability, avoidance of errors. There are better control structures for humans; goto is for machine generated code.
面向对象思考
2020/06/04
5970
C++核心准则ES.2: 适当的抽象好于直接使用语言功能
A "suitable abstraction" (e.g., library or class) is closer to the application concepts than the bare language, leads to shorter and clearer code, and is likely to be better tested.
面向对象思考
2020/04/17
3650
C++核心准则E.13: 直接拥有一个对象所有权时永远不要抛出异常
E.13: Never throw while being the direct owner of an object
面向对象思考
2020/08/04
3460
C++核心准则​讨论:持有没有被句柄管理的资源时切勿抛出异常
Discussion: Never throw while holding a resource not owned by a handle
面向对象思考
2020/12/31
7060
C++核心准则C.30:如果一个类需要明确的销毁动作,定义析构函数
A destructor is implicitly invoked at the end of an object's lifetime. If the default destructor is sufficient, use it. Only define a non-default destructor if a class needs to execute code that is not already part of its members' destructors.
面向对象思考
2020/03/25
3850
C++核心准则R.12:立即将显式分配的资源交给资源管理对象​
If you don't, an exception or a return may lead to a leak.
面向对象思考
2020/03/31
2760
C++核心准则R.12:立即将显式分配的资源交给资源管理对象​
C++核心准则 讨论:“原始”指针或引用绝对不是资源句柄
To be able to distinguish owners from views.
面向对象思考
2020/12/31
3920
C++核心准则 讨论:“原始”指针或引用绝对不是资源句柄
C++核心准则C102-109:容器的基本要求
Containers tend to get large; without a move constructor and a copy constructor an object can be expensive to move around, thus tempting people to pass pointers to it around and getting into resource management problems.
面向对象思考
2020/03/25
2750
C++核心准则E.3:异常应该只用于错误处理
To keep error handling separated from "ordinary code." C++ implementations tend to be optimized based on the assumption that exceptions are rare.
面向对象思考
2020/07/28
2680
C++核心准则​讨论:切勿让指针的生命周期超出其指向的对象
Discussion: Never let a pointer outlive the object it points to
面向对象思考
2020/12/31
7170
C++核心准则​讨论:切勿让指针的生命周期超出其指向的对象
C++核心准则C.139:谨慎使用final
Capping a hierarchy with final is rarely needed for logical reasons and can be damaging to the extensibility of a hierarchy.
面向对象思考
2020/03/25
4630
C++核心准则​NR.3:不要拒绝使用异常
There seem to be four main reasons given for not using exceptions:
面向对象思考
2020/11/10
4950
C++核心准则​NR.3:不要拒绝使用异常
推荐阅读
相关推荐
C++核心准则​NR.6:不要将所有清理操作放在函数最后并使用goto语句跳转
更多 >
领券
💥开发者 MCP广场重磅上线!
精选全网热门MCP server,让你的AI更好用 🚀
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验