前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则SF.11​:头文件应该可以独立工作​

C++核心准则SF.11​:头文件应该可以独立工作​

作者头像
面向对象思考
发布2020-10-30 11:25:47
3760
发布2020-10-30 11:25:47
举报
文章被收录于专栏:C++核心准则原文翻译

SF.11: Header files should be self-contained

SF.11:头文件应该可以独立工作

Reason(原因)

Usability, headers should be simple to use and work when included on their own. Headers should encapsulate the functionality they provide. Avoid clients of a header having to manage that header's dependencies.

易用性,当只有它自己被包含时,头文件应该也容易使用和动作。头文件应该封装它们所提供的功能。避免头文件的用户不得不处理头文件之间的依赖关系。

Example(示例)

代码语言:javascript
复制
#include "helpers.h"
// helpers.h depends on std::string and includes <string>
Note(注意)

Failing to follow this results in difficult to diagnose errors for clients of a header.

违反本准则的结果是头文件的用户难以诊断发生的错误。(因为前提是弄清楚头文件之间的依赖关系,译者注。)

Note(注意)

A header should include all its dependencies. Be careful about using relative paths because C++ implementations diverge on their meaning.

头文件应该包含所有的依赖关系。由于C++的具体实现之间的处理方式不同,因此需要小心地使用相对路径。

Enforcement(实施建议)

A test should verify that the header file itself compiles or that a cpp file which only includes the header file compiles.

检查编译的头文件或者只包含编译的头文件的cpp文件。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#sf11-header-files-should-be-self-contained

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SF.11: Header files should be self-contained
  • Reason(原因)
    • Note(注意)
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档