SF.11:头文件应该可以独立工作
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(示例)
#include "helpers.h"
// helpers.h depends on std::string and includes <string>
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