{"id":5670,"date":"2019-04-25T19:11:03","date_gmt":"2019-04-25T19:11:03","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-the-remaining-rules-to-source-files\/"},"modified":"2023-06-26T10:11:37","modified_gmt":"2023-06-26T10:11:37","slug":"c-core-guidelines-the-remaining-rules-to-source-files","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-the-remaining-rules-to-source-files\/","title":{"rendered":"C++ Core Guidelines: The Remaining Rules about Source Files"},"content":{"rendered":"<p>Today, I complet the rules of the C++ core guidelines to source files. They are about header files and namespaces.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5668\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/vga-160266_1280.png\" alt=\"vga 160266 1280\" width=\"400\" height=\"384\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/vga-160266_1280.png 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/vga-160266_1280-300x288.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/vga-160266_1280-1024x982.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/vga-160266_1280-768x737.png 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Let&#8217;s see how I can make a story out of the remaining rules because they do have little content. Anyway, here are they:<\/p>\n<ul>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-guards\">SF.8: Use <code class=\"highlighter-rouge no-highlight\">#include<\/code> guards for all <code class=\"highlighter-rouge no-highlight\">.h<\/code> files<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-cycles\">SF.9: Avoid cyclic dependencies among source files<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-implicit\">SF.10: Avoid dependencies on implicitly <code class=\"highlighter-rouge no-highlight\">#include<\/code>d names<\/a><\/li>\n<li>\n<p><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-contained\">SF.11: Header files should be self-contained<\/a><\/p>\n<\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-namespace\">SF.20: Use <code class=\"highlighter-rouge no-highlight\">namespace<\/code>s to express logical structure<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-unnamed\">SF.21: Don\u2019t use an unnamed (anonymous) namespace in a header<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-unnamed2\">SF.22: Use an unnamed (anonymous) namespace for all internal\/nonexported entities<\/a><\/li>\n<\/ul>\n<p>The first rule is already best practice.<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-guards\">SF.8: Use <code class=\"highlighter-rouge no-highlight\">#include<\/code> guards for all <code class=\"highlighter-rouge no-highlight\">.h<\/code> files<\/a><\/h2>\n<p>When you put an include guard around your header file, it is only included once. Here is a small example from the guidelines.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ file foobar.h:<\/span>\r\n<span style=\"color: #009999;\">#ifndef LIBRARY_FOOBAR_H<\/span>\r\n<span style=\"color: #009999;\">#define LIBRARY_FOOBAR_H<\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ ... declarations ...<\/span>\r\n<span style=\"color: #009999;\">#endif <\/span><span style=\"color: #0099ff; font-style: italic;\">\/\/ LIBRARY_FOOBAR_H<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>There are two points to keep in mind.<\/p>\n<ol>\n<li>Give your guard a unique name. If you use a guard name more than once, it may exclude the inclusion of a header file.<\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Pragma_once\">#pragma once<\/a> is a non-standard but widely supported preprocessor directive. This means the following variation of the header <span style=\"font-family: courier new, courier;\">foobar.h<\/span> is not portable.<\/li>\n<\/ol>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ file foobar.h:<\/span>\r\n<span style=\"color: #009999;\">#pragma once<\/span>\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ ... declarations ...<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>For example, GCC has supported the preprocessor directive since 3.4. Read the details about the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pragma_once\">#pragma once<\/a> supported in the Wikipedia article.<\/p>\n<p>Okay, the following rule is tricky.<\/p>\n<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-cycles\">SF.9: Avoid cyclic dependencies among source files<\/a><\/h2>\n<p>First of all, what is a cyclic dependency of source files? Imagine, I have the following source files.<\/p>\n<ul>\n<li>&nbsp;<span style=\"font-family: courier new, courier;\">a.h<\/span><\/li>\n<\/ul>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #009999;\">#ifndef LIBRARY_A_H<\/span>\r\n<span style=\"color: #009999;\">#define LIBRARY_A_H<\/span>\r\n<span style=\"color: #009999;\">#include \"b.h\"<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">A<\/span> {\r\n  B<span style=\"color: #555555;\"><\/span> b;\r\n};\r\n\r\n<span style=\"color: #009999;\">#endif <\/span><span style=\"color: #0099ff; font-style: italic;\">\/\/ LIBRARY_A_H<\/span>\r\n<\/pre>\n<\/div>\n<ul>\n<li><span style=\"font-family: courier new, courier;\">b.h<\/span><\/li>\n<\/ul>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #009999;\">#ifndef LIBRARY_B_H<\/span>\r\n<span style=\"color: #009999;\">#define LIBRARY_B_H<\/span>\r\n<span style=\"color: #009999;\">#include \"a.h\"<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">B<\/span> {\r\n  A a;\r\n};\r\n\r\n<span style=\"color: #009999;\">#endif <\/span><span style=\"color: #0099ff; font-style: italic;\">\/\/ LIBRARY_B_H<\/span>\r\n<\/pre>\n<\/div>\n<ul>\n<li><span style=\"font-family: courier new, courier;\">main.cpp<\/span><\/li>\n<\/ul>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #009999;\">#include \"a.h\"<\/span><span style=\"color: #009999;\"><\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n  A a;\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>When I try to compile the program, it fails:<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4809\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/dependency.png\" alt=\"dependency\" width=\"350\" height=\"182\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>The issue is a circular dependency between the headers <span style=\"font-family: courier new, courier;\">a.h<\/span> and <span style=\"font-family: courier new, courier;\">b.h<\/span>. The problem manifests itself when <span style=\"font-family: courier new, courier;\">a<\/span> is created in the main program. To create an A, the compiler must figure out the size of <span style=\"font-family: courier new, courier;\">B<\/span>. To create a <span style=\"font-family: courier new, courier;\">B<\/span>, the compiler must figure out the size of <span style=\"font-family: courier new, courier;\">A<\/span>. This is not possible if <span style=\"font-family: courier new, courier;\">a<\/span> or <span style=\"font-family: courier new, courier;\">b<\/span> are objects. It is only possible if <span style=\"font-family: courier new, courier;\">a<\/span> or <span style=\"font-family: courier new, courier;\">b<\/span> are pointers or references.<\/p>\n<p>The easy fix is, therefore, to <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/class\">forward declare<\/a> <span style=\"font-family: courier new, courier;\">A<\/span> in <span style=\"font-family: courier new, courier;\">b.h<\/span> or <span style=\"font-family: courier new, courier;\">B<\/span> in <span style=\"font-family: courier new, courier;\">a.h<\/span>. Depending on your platform, the size is now 32 or 64 bits.&nbsp; Here is the modified header of <span style=\"font-family: courier new, courier;\">a.h<\/span>.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #009999;\">#ifndef LIBRARY_A_H<\/span>\r\n<span style=\"color: #009999;\">#define LIBRARY_A_H<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">B<\/span>;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">A<\/span> {\r\n  B<span style=\"color: #555555;\">*<\/span> b;\r\n  B<span style=\"color: #555555;\">&amp;<\/span> b2 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">*<\/span>b;\r\n};\r\n\r\n<span style=\"color: #009999;\">#endif <\/span><span style=\"color: #0099ff; font-style: italic;\">\/\/ LIBRARY_A_H<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>By the way, the standard library header <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/header\/iosfwd\"><span style=\"font-family: courier new, courier;\">&lt;iosfwd&gt;<\/span><\/a> holds forward input\/output library declarations.<\/p>\n<p>The next issue happened to me a few times.<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-implicit\">SF.10: Avoid dependencies on implicitly <code class=\"highlighter-rouge no-highlight\">#include<\/code>d names<\/a><\/h2>\n<p>For example, the following program will compile with GCC 5.4 but break with the Microsoft compiler 19.00.23506.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>string s <span style=\"color: #555555;\">=<\/span> <span style=\"color: #cc3300;\">\"Hello World\"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> s;\r\n\r\n}\r\n&nbsp;<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I forgot to include a necessary header <span style=\"font-family: courier new, courier;\">&lt;string&gt;<\/span>. GCC 5.4 includes <span style=\"font-family: courier new, courier;\">&lt;string&gt;<\/span> with the header <span style=\"font-family: courier new, courier;\">&lt;iostream&gt;.&nbsp;<\/span>This does not hold true for the Microsoft compiler. The error message of the Microsoft compiler is quite verbose.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5669\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/forgotString.png\" alt=\"forgotString\" width=\"700\" height=\"285\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/forgotString.png 1775w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/forgotString-300x122.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/forgotString-1024x417.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/forgotString-768x312.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/forgotString-1536x625.png 1536w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>The following rule is short but essential.<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-contained\">SF.11: Header files should be self-contained<\/a><\/h2>\n<p>A self-contained header file can be included <em>top-most<\/em> in a translation unit. This means it does not depend on other headers that are included before. If you don&#8217;t follow this rule, a user of your header may be surprised by difficult-to-understand error messages. Sometimes the header seems to work; sometimes not. It just depends on which header was included before.<\/p>\n<p>The last three rules to source files are about namespaces. It starts with a no-brainer.<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-namespace\">SF.20: Use <code class=\"highlighter-rouge no-highlight\">namespace<\/code>s to express logical structure<\/a><\/h2>\n<p>Obviously, we have namespaces in the C++ standard to express logical structure. Examples? Here are a few:<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">std\r\nstd<span style=\"color: #555555;\">::<\/span>chrono\r\nstd<span style=\"color: #555555;\">::<\/span>literals<br \/>std::literals::chrono_literals\r\nstd<span style=\"color: #555555;\">::<\/span>filesystem<br \/>std::placeholders\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>view      <span style=\"color: #0099ff; font-style: italic;\">\/\/ C++20<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The next two rules are about unnamed (anonymous) namespaces.<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-unnamed\">SF.21: Don\u2019t use an unnamed (anonymous) namespace in a header, <\/a>and <a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-unnamed2\">SF.22: Use an unnamed (anonymous) namespace for all internal\/nonexported entities<\/a><\/h2>\n<p>An unnamed namespace has internal linkage. This means that names inside the unnamed namespace can only be referred from within the current translation unit and are not exported (<a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-unnamed2\">SF22<\/a>). The same holds for namespaces, which are declared in the unnamed namespace. Okay, what does that mean?<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">namespace<\/span> {\r\n    <span style=\"color: #007788; font-weight: bold;\">int i<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ defines ::(unique_name)::i<\/span>\r\n}\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> inc() {\r\n    i<span style=\"color: #555555;\">++<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ increments ::(unique_name)::i<\/span>\r\n}\r\n \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>When you refer to I from within the translation unit, you do it by a <span style=\"font-family: courier new, courier;\">unique_name <\/span>without a name clash. For example, you can define a free addition function <span style=\"font-family: courier new, courier;\">add<\/span> inside the unnamed namespace, and the linker would not complain because you broke the one definition rule.<\/p>\n<p>And here is the problem, when you use an unnamed namespace in the header (<a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-unnamed\">SF21<\/a>). Each translation unit will define its unique instance of the unnamed namespace. Unnamed namespaces in headers have a few consequences:<\/p>\n<ul>\n<li>The resulting executable will bloat.<\/li>\n<li>Any declaration in an unnamed namespace refers to a different entity in each translation unit. This may not be the expected behavior.<\/li>\n<\/ul>\n<p>The usage of an unnamed namespace is similar to the <span style=\"font-family: courier new, courier;\">static<\/span> keyword used in C.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">namespace<\/span> { <span style=\"color: #007788; font-weight: bold;\">int<\/span> i1; }\r\n<span style=\"color: #006699; font-weight: bold;\">static<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> i2;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>The C++ core guidelines mentioned modules in the rules to source files a few times, but they didn&#8217;t write about the big C++20 feature. Let me fill the gap in my <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-20-modules\">next post.<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I complet the rules of the C++ core guidelines to source files. They are about header files and namespaces.<\/p>\n","protected":false},"author":21,"featured_media":5668,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[473],"class_list":["post-5670","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-source-files"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5670"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5670\/revisions"}],"predecessor-version":[{"id":6790,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5670\/revisions\/6790"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5668"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}