{"id":5666,"date":"2019-04-16T05:35:15","date_gmt":"2019-04-16T05:35:15","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-source-files\/"},"modified":"2023-06-26T10:11:51","modified_gmt":"2023-06-26T10:11:51","slug":"c-core-guidelines-source-files","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-source-files\/","title":{"rendered":"C++ Core Guidelines: Source Files"},"content":{"rendered":"<p>The organization of source files is a topic quite seldom addressed in C++. With C++20, we will get modules, but until then, we should distinguish between our code&#8217;s implementation and interface.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5662\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/serial-160834_1280.png\" alt=\"serial 160834 1280\" width=\"500\" height=\"250\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/serial-160834_1280.png 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/serial-160834_1280-300x150.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/serial-160834_1280-1024x512.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/serial-160834_1280-768x384.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>The C++ Core Guidelines make their point to source files quite clear: &#8220;Distinguish between declarations (used as interfaces) and definitions (used as implementations). Use header files to represent interfaces and to emphasize logical structure.&#8221; Consequently, there are more than ten rules to source files. The first eleven rules deal with interface files<span style=\"font-family: courier new, courier;\"> (*.h<\/span>-files<span style=\"font-family: courier new, courier;\">)<\/span> and implementation files (<span style=\"font-family: courier new, courier;\">*.cpp-<\/span>files), and the last three with namespaces.<\/p>\n<p>Let me start with the rules for the interface and implementation files. Here are the first seven:<\/p>\n<ul>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-file-suffix\">SF.1: Use a <code class=\"highlighter-rouge no-highlight\">.cpp<\/code> suffix for code files and <code class=\"highlighter-rouge no-highlight\">.h<\/code> for interface files if your project doesn\u2019t already follow another convention<\/a><\/li>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-inline\">SF.2: A <code class=\"highlighter-rouge no-highlight\">.h<\/code> file may not contain object definitions or non-inline function definitions<\/a><\/li>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-declaration-header\">SF.3: Use <code class=\"highlighter-rouge no-highlight\">.h<\/code> files for all declarations used in multiple source files<\/a><\/li>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-include-order\">SF.4: Include <code class=\"highlighter-rouge no-highlight\">.h<\/code> files before other declarations in a file<\/a><\/li>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-consistency\">SF.5: A <code class=\"highlighter-rouge no-highlight\">.cpp<\/code> file must include the <code class=\"highlighter-rouge no-highlight\">.h<\/code> file(s) that defines its interface<\/a><\/li>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-using\">SF.6: Use <code class=\"highlighter-rouge no-highlight\">using namespace<\/code> directives for transition, for foundation libraries (such as <code class=\"highlighter-rouge no-highlight\">std<\/code>), or within a local scope (only)<\/a><\/li>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-using-directive\">SF.7: Don\u2019t write <code class=\"highlighter-rouge no-highlight\">using namespace<\/code> at global scope in a header file<\/a><\/li>\n<\/ul>\n<p>I will not write about each rule in full depth, but I want to make a readable story out of the first rules by just quoting the rule.<\/p>\n<p>Okay, <a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-file-suffix\">SF.1: Use a <code class=\"highlighter-rouge no-highlight\">.cpp<\/code> suffix for code files and <code class=\"highlighter-rouge no-highlight\">.h<\/code> for interface files if your project doesn\u2019t already follow another convention <\/a>about consistency. When you have a C++ project, header files should be called <span style=\"font-family: courier new, courier;\">*.h<\/span> and implementation files <span style=\"font-family: courier new, courier;\">*.cpp. <\/span>Convention beats this rule if you already have another policy in our project.&nbsp;<\/p>\n<p>Of course, I often saw other conventions for header and implementation files. Here are a few I have in mind:<\/p>\n<ul>\n<li>Header files:\n<ul>\n<li><span style=\"font-family: courier new, courier;\">*.h<\/span><\/li>\n<li><span style=\"font-family: courier new, courier;\">*.hpp<\/span><\/li>\n<li><span style=\"font-family: courier new, courier;\">*.hxx<\/span><\/li>\n<\/ul>\n<\/li>\n<li>Implementation files:\n<ul>\n<li><span style=\"font-family: courier new, courier;\">*.cpp<\/span><\/li>\n<li><span style=\"font-family: courier new, courier;\">*.c<\/span><\/li>\n<li><span style=\"font-family: courier new, courier;\">*.cc<\/span><\/li>\n<li><span style=\"font-family: courier new, courier;\">*.cxx<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>I assume you know various other conventions.<\/p>\n<p>If your header file contains an object definition or a definition of a non-inline function, your linker may complain. This is the reason for the second rule <a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-inline\">SF.2: A <code class=\"highlighter-rouge no-highlight\">.h<\/code> file may not contain object definitions or non-inline function definitions. <\/a>To be more specific, we have the One Definition Rule in C++:<\/p>\n<\/p>\n<h3>ODR<\/h3>\n<p>ODR stands for the One Definition Rule and says in the case of a function.<\/p>\n<ul>\n<li>A function can have not more than one definition in any translation unit.<\/li>\n<li>A function can have not more than one definition in the program.<\/li>\n<li>Inline functions with external linkage can be defined in more than one translation. The definitions must satisfy the requirement that each must be the same.<\/li>\n<\/ul>\n<p>In modern compilers, the keyword <span style=\"font-family: courier new, courier;\">inline<\/span> is not about inlining functions anymore. Modern compilers almost wholly ignore it. The more or less use-case for <span style=\"font-family: courier new, courier;\">inline<\/span> is to mark functions for ODR correctness. In my opinion, the name <span style=\"font-family: courier new, courier;\">inline<\/span> is nowadays quite misleading.&nbsp;<\/p>\n<p>Let me see what my linker says when I try to link a program breaking the one-definition rule. The following code example has one header file header.h and two implementation files. The implementations file includes the header files and breaks the one definition rule because of two definitions of <span style=\"font-family: courier new, courier;\">func<\/span> exit.<\/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;\">\/\/ header.h<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">func<\/span>(){}\r\n<\/pre>\n<\/div>\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: #0099ff; font-style: italic;\">\/\/ impl.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include \"header.h\"<\/span>\r\n<\/pre>\n<\/div>\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: #0099ff; font-style: italic;\">\/\/ main.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include \"header.h\"<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The linker complains about the multiple definitions of <span style=\"font-family: courier new, courier;\">func<\/span>:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5663\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/odr.png\" alt=\"odr\" width=\"450\" height=\"145\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/odr.png 1144w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/odr-300x97.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/odr-1024x330.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/odr-768x248.png 768w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>The following two rules are evident from the readability and maintainability point of view: <a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-declaration-header\">SF.3: Use <code class=\"highlighter-rouge no-highlight\">.h<\/code> files for all declarations used in multiple source files<\/a> and <a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-include-order\">SF.4: Include <code class=\"highlighter-rouge no-highlight\">.h<\/code> files before other declarations in a file.<\/a><\/p>\n<p>Rule 5 is more interesting: <a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-consistency\">SF.5: A <code class=\"highlighter-rouge no-highlight\">.cpp<\/code> file must include the <code class=\"highlighter-rouge no-highlight\">.h<\/code> file(s) that defines its interface<\/a>. The interesting question is: What would happen if you don&#8217;t include the <span style=\"font-family: courier new, courier;\">*.h<\/span> file in the *.cpp file and there is a mismatch between the interface file <span style=\"font-family: courier new, courier;\">*.h<\/span> and the implementation file <span style=\"font-family: courier new, courier;\">*.cpp<\/span>?.<\/p>\n<p>Assume I had a bad day. I defined a function <span style=\"font-family: courier new, courier;\">func<\/span> that gets an <span style=\"font-family: courier new, courier;\">int<\/span> and returns an <span style=\"font-family: courier new, courier;\">int.<\/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: #0099ff; font-style: italic;\">\/\/ impl.cpp<\/span>\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ #include \"impl.h\" <\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">func<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span>){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">5<\/span>;\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>My mistake was that I declared this function in the header file <span style=\"font-family: courier new, courier;\">impl.h<\/span> getting an <span style=\"font-family: courier new, courier;\">int<\/span> but returning a <span style=\"font-family: courier new, courier;\">std::string<\/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: #0099ff; font-style: italic;\">\/\/ impl.h<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>string func(<span style=\"color: #007788; font-weight: bold;\">int<\/span>);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I include the header in the <span style=\"font-family: courier new, courier;\">main<\/span> program because I want to use this function there.<\/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: #0099ff; font-style: italic;\">\/\/ main.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include \"impl.h\"<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> res <span style=\"color: #555555;\">=<\/span> func(<span style=\"color: #ff6600;\">5<\/span>);\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The issue is that the error may be delayed until link time when the main program <span style=\"font-family: courier new, courier;\">main.cpp <\/span>is compiled. This is too late.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5664\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/linker.png\" alt=\"linker\" width=\"500\" height=\"133\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/linker.png 1266w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/linker-300x80.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/linker-1024x273.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/linker-768x204.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>If I include the header<span style=\"font-family: courier new, courier;\"> impl.h<\/span> in my <span style=\"font-family: courier new, courier;\">impl.cpp<\/span> file, I will get a compile-time error.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5665\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/compiler.png\" alt=\"compiler\" width=\"500\" height=\"183\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/compiler.png 1222w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/compiler-300x110.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/compiler-1024x375.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/compiler-768x281.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>The following rules are about namespaces:&nbsp;<a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-using\">SF.6: Use <code class=\"highlighter-rouge no-highlight\">using namespace<\/code> directives for transition for foundation libraries (such as <code class=\"highlighter-rouge no-highlight\">std<\/code>), or within a local scope (only)<\/a>. Honestly, this rule is too weak for me. I&#8217;m against using namespaces directives such as in the following example.<\/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;\">#include &lt;cmath&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> std;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">g<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> x)\r\n{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> sqrt <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">7<\/span>;\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> sqrt(x); <span style=\"color: #0099ff; font-style: italic;\">\/\/ error<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The program will not compile because there is a name clash. This is not my main argument against<span style=\"font-family: courier new, courier;\"> using<\/span> directives. My main argument is that the<span style=\"font-family: courier new, courier;\"> using<\/span> directive hides the name&#8217;s origin and breaks the code&#8217;s readability.<\/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<span style=\"color: #009999;\">#include &lt;chrono&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> std;\r\n<span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> std<span style=\"color: #555555;\">::<\/span>chrono;\r\n<span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> std<span style=\"color: #555555;\">::<\/span>literals<span style=\"color: #555555;\">::<\/span>chrono_literals;\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>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> schoolHour<span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">45<\/span>min;\r\n\r\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> shortBreak<span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">300<\/span>s;\r\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> longBreak<span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0.25<\/span>h;\r\n\r\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> schoolWay<span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">15<\/span>min;\r\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> homework<span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>h;\r\n\r\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> schoolDayInSeconds<span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span> <span style=\"color: #555555;\">*<\/span> schoolWay <span style=\"color: #555555;\">+<\/span> <span style=\"color: #ff6600;\">6<\/span> <span style=\"color: #555555;\">*<\/span> schoolHour <span style=\"color: #555555;\">+<\/span> <span style=\"color: #ff6600;\">4<\/span> <span style=\"color: #555555;\">*<\/span> shortBreak <span style=\"color: #555555;\">+<\/span> longBreak <span style=\"color: #555555;\">+<\/span> homework;\r\n\r\n  <span style=\"color: #555555;\"><\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"School day in seconds: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> schoolDayInSeconds.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> endl;\r\n\r\n  duration<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span>, ratio<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">3600<\/span><span style=\"color: #555555;\">&gt;&gt;<\/span> schoolDayInHours <span style=\"color: #555555;\">=<\/span> schoolDayInSeconds;\r\n  duration<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span>, ratio<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">60<\/span><span style=\"color: #555555;\">&gt;&gt;<\/span> schoolDayInMinutes <span style=\"color: #555555;\">=<\/span> schoolDayInSeconds;\r\n  duration<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span>, ratio<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">1000<\/span><span style=\"color: #555555;\">&gt;&gt;<\/span> schoolDayInMilliseconds <span style=\"color: #555555;\">=<\/span> schoolDayInSeconds;\r\n\r\n  cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"School day in hours: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> schoolDayInHours.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> endl;\r\n  cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"School day in minutes: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> schoolDayInMinutes.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> endl;\r\n  cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"School day in milliseconds: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> schoolDayInMilliseconds.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> endl;\r\n\r\n  cout <span style=\"color: #555555;\">&lt;&lt;<\/span> endl;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Do you know by heart which literal, function, or object was defined in which namespace? If not, looking for the definition of a name may become a challenge. This holds, in particular, true if you are a novice.<\/p>\n<p>Before I end this post, there is one import rule I have to mention: <a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rs-using-directive\">SF.7: Don\u2019t write <code class=\"highlighter-rouge no-highlight\">using namespace<\/code> at global scope in a header file<\/a>. Here is the rationale:<\/p>\n<p>A using namespace at global scope in the header injects names into every file that includes that header. This has a few consequences:<\/p>\n<ul>\n<li>When you use the header, you can not undo the using directive.<\/li>\n<li>The danger of a name collision increases drastically.<\/li>\n<li>Changing the included namespace may break your build because a new name was introduced.<\/li>\n<\/ul>\n<h2>What&#8217;s next?<\/h2>\n<p>First, a few rules for the organization of source files are left. Additionally, we will get modules with C++20. Let&#8217;s see which effect these significant features have on C++-<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<div id=\"s3gt_translate_tooltip_mini\" class=\"s3gt_translate_tooltip_mini_box\" style=\"background: initial !important; border: initial !important; border-radius: initial !important; border-spacing: initial !important; border-collapse: initial !important; direction: ltr !important; flex-direction: initial !important; font-weight: initial !important; height: initial !important; letter-spacing: initial !important; min-width: initial !important; max-width: initial !important; min-height: initial !important; max-height: initial !important; margin: auto !important; outline: initial !important; padding: initial !important; position: absolute; table-layout: initial !important; text-align: initial !important; text-shadow: initial !important; width: initial !important; word-break: initial !important; word-spacing: initial !important; overflow-wrap: initial !important; box-sizing: initial !important; display: initial !important; color: inherit !important; font-size: 13px !important; font-family: X-LocaleSpecific, sans-serif, Tahoma, Helvetica !important; line-height: 13px !important; vertical-align: top !important; white-space: inherit !important; left: 916px; top: 35px; opacity: 0.8;\">&nbsp;<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The organization of source files is a topic quite seldom addressed in C++. With C++20, we will get modules, but until then, we should distinguish between our code&#8217;s implementation and interface.<\/p>\n","protected":false},"author":21,"featured_media":5662,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[473],"class_list":["post-5666","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\/5666","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=5666"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5666\/revisions"}],"predecessor-version":[{"id":6791,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5666\/revisions\/6791"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5662"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}