Skip to content

Commit e47463c

Browse files
authored
SF.12: Prefer the quoted form of #include for files relative to the including file
The current guidance on SF.12 can be over-applied and devolves into "always use <>" because all compilers support adding include directories to the <> search. In this case, even the current directory may be added and so it is always possible to use <> for every header. Applying the guidance then devolves into an undesirable state where <> is always used and include"" is never used. Instead, the proposed guidance leverages and encourages the distinction between <> and "" to create an easy-to-understand rule that the original guidance hints at and that most developers already follow and understand: "" is for local headers and <> is for library and external headers.
1 parent 9928c95 commit e47463c

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

CppCoreGuidelines.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18743,7 +18743,7 @@ Source file rule summary:
1874318743
* [SF.9: Avoid cyclic dependencies among source files](#Rs-cycles)
1874418744
* [SF.10: Avoid dependencies on implicitly `#include`d names](#Rs-implicit)
1874518745
* [SF.11: Header files should be self-contained](#Rs-contained)
18746-
* [SF.12: Prefer the angle bracket form of `#include` where you can and the quoted form everywhere else](#Rs-incform)
18746+
* [SF.12: Prefer the quoted form of `#include` for files relative to the including file and the angle bracket form everywhere else](#Rs-incform)
1874718747

1874818748
* [SF.20: Use `namespace`s to express logical structure](#Rs-namespace)
1874918749
* [SF.21: Don't use an unnamed (anonymous) namespace in a header](#Rs-unnamed)
@@ -19182,23 +19182,22 @@ A header should include all its dependencies. Be careful about using relative pa
1918219182

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

19185-
### <a name="Rs-incform"></a>SF.12: Prefer the angle bracket form of `#include` where you can and the quoted form everywhere else
19185+
### <a name="Rs-incform"></a>SF.12: Prefer the quoted form of `#include` for files relative to the including file and the angle bracket form everywhere else
1918619186

1918719187
##### Reason
1918819188

1918919189
The [standard](http://eel.is/c++draft/cpp.include) provides flexibility for compilers to implement
1919019190
the two forms of `#include` selected using the angle (`<>`) or quoted (`""`) syntax. Vendors take
1919119191
advantage of this and use different search algorithms and methods for specifying the include path.
1919219192

19193-
Nevertheless, the guidance is to use the angle form when possible. This supports the fact that the
19194-
standard library headers must be included this way, is more likely to create portable code, and enables
19195-
the quoted form for other uses. For example being clear about the locality of the header relative
19196-
to files that includes it or in scenarios where the different search algorithm is required.
19193+
Nevertheless, the guidance is to use quoted form for including files that exist at a relative path to the file containing the #include statement and to use the angle bracket form everywhere else where possible. This supports being clear about the locality of the header relative to files that includes it or in scenarios where the different search algorithm is required. For example, it makes it easy to determine at a glance whether a header is being included from a local relative file versus a standard library header or an external header from another project.
1919719194

1919819195
##### Example
19199-
19196+
foo.cpp:
1920019197
#include <string> // From the standard library, required form
19201-
#include "helpers.h" // A project specific file, use "" form
19198+
#include <some_library/common.h> //A non-local include file from an external library, use the <> form
19199+
#include "foo.h" // A local file relative to foo.cpp, use "" form
19200+
#include "utils/foo_utils.h" // A local file relative to foo.cpp, use "" form
1920219201

1920319202
##### Note
1920419203
Failing to follow this results in difficult to diagnose errors due to picking up the wrong file by incorrectly specifying the scope when it is included.

0 commit comments

Comments
 (0)