Skip to content

Commit 6f50150

Browse files
authored
Merge pull request #1596 from apenn-msft/patch-1
SF.12: Prefer the quoted form of `#include` for files relative to the…
2 parents d996c97 + 94d6123 commit 6f50150

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

CppCoreGuidelines.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -18794,7 +18794,7 @@ Source file rule summary:
1879418794
* [SF.9: Avoid cyclic dependencies among source files](#Rs-cycles)
1879518795
* [SF.10: Avoid dependencies on implicitly `#include`d names](#Rs-implicit)
1879618796
* [SF.11: Header files should be self-contained](#Rs-contained)
18797-
* [SF.12: Prefer the angle bracket form of `#include` where you can and the quoted form everywhere else](#Rs-incform)
18797+
* [SF.12: Prefer the quoted form of `#include` for files relative to the including file and the angle bracket form everywhere else](#Rs-incform)
1879818798

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

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

19239-
### <a name="Rs-incform"></a>SF.12: Prefer the angle bracket form of `#include` where you can and the quoted form everywhere else
19239+
### <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
1924019240

1924119241
##### Reason
1924219242

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

19247-
Nevertheless, the guidance is to use the angle form when possible. This supports the fact that the
19248-
standard library headers must be included this way, is more likely to create portable code, and enables
19249-
the quoted form for other uses. For example being clear about the locality of the header relative
19250-
to files that includes it or in scenarios where the different search algorithm is required.
19247+
Nevertheless, the guidance is to use the quoted form for including files that exist at a relative path to the file containing the `#include` statement (from within the same component or project) and to use the angle bracket form everywhere else, where possible. This encourages being clear about the locality of the file relative to files that include it, or scenarios where the different search algorithm is required. It makes it easy to understand at a glance whether a header is being included from a local relative file versus a standard library header or a header from the alternate search path (e.g. a header from another library or a common set of includes).
1925119248

1925219249
##### Example
1925319250

19254-
#include <string> // From the standard library, required form
19255-
#include "helpers.h" // A project specific file, use "" form
19251+
// foo.cpp:
19252+
#include <string> // From the standard library, requires the <> form
19253+
#include <some_library/common.h> // A file that is not locally relative, included from another library; use the <> form
19254+
#include "foo.h" // A file locally relative to foo.cpp in the same project, use the "" form
19255+
#include "foo_utils/utils.h" // A file locally relative to foo.cpp in the same project, use the "" form
1925619256

1925719257
##### Note
1925819258

19259-
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.
19259+
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. For example, in a typical case where the `#include ""` search algorithm may search for a file existing at a local relative path first, then using this form to refer to a file that is not locally relative could mean that if a file ever comes into existence at the local relative path (e.g. the including file is moved to a new location), it will now be found ahead of the previous include file and the set of includes will have been changed in an unexpected way.
1926019260

1926119261
Library creators should put their headers in a folder and have clients include those files using the relative path `#include <some_library/common.h>`
1926219262

0 commit comments

Comments
 (0)