{"id":313,"date":"2019-02-14T10:43:29","date_gmt":"2019-02-14T18:43:29","guid":{"rendered":"http:\/\/35.243.195.209\/?p=313"},"modified":"2019-12-30T14:44:48","modified_gmt":"2019-12-30T22:44:48","slug":"namespace-and-using","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2019\/02\/14\/namespace-and-using\/","title":{"rendered":"Namespace and Using"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>In this post, I will introduce how to use &quot;using&quot; in C++, in other words, how to use an existing namespace. <\/p>\n<h2>Conclusion<\/h2>\n<p>As usual, I present the conclusion first. The followings are recommended ways which minimize name collision possibilities and maximize code clarity and writing convenience.<\/p>\n<p>See <a href=\"http:\/\/161.97.122.139\/index.php\/2019\/12\/30\/edge-cases-of-namespace-pollution\/\">this post<\/a> as well. <\/p>\n<h3>For Headers<\/h3>\n<ol>\n<li>Do not put any namespace using statement at the top level in headers;<\/li>\n<li>Use fully qualified names (for example, std::string for Standard Library names) in headers. Or you can use narrowly-scoped namespace using statements.  <\/li>\n<\/ol>\n<h3>For Source Files<\/h3>\n<ol>\n<li>Put namespace using statements in local scopes;<\/li>\n<li>Do not use fully qualified names everywhere;<\/li>\n<li>Do not use <code>using namespace std<\/code>, but use specific using declarations for just the names you need (for example, <code>using std::string<\/code> and <code>using std::cin; using std::cout; using std::endl;<\/code>);<\/li>\n<\/ol>\n<h2>Details<\/h2>\n<h3>For Headers<\/h3>\n<p><strong>Rule 1<\/strong>: Do not put any namespace using statement at the top level in headers.<br \/>\nReason: If you do so, when other programmers include your header, they can\u2019t override it with their own, which increases collision possibilities.<\/p>\n<p><strong>Rule 2<\/strong>: Use fully qualified names.<br \/>\nReason: Based on Rule 1, you have to use fully qualified names in headers. <\/p>\n<h3>For Source Files<\/h3>\n<p><strong>Rule 1<\/strong>: Put namespace using statements after all &quot;#includes &#8230;&quot;.<br \/>\nReason: If you do so, the compliler will put the using namespace std before &quot;onnx_loader.h&quot;, which increases collision possibilities.<\/p>\n<pre><code class=\"language-c++\">#include &lt;iostream&gt;\nusing namespace std; \n#include &lt;onnx_loader.h&gt;<\/code><\/pre>\n<pre><code class=\"language-c++\">#include &lt;iostream&gt;\n#include &lt;onnx_loader.h&gt;\n\/\/ all other #include\u2019s\nusing namespace std; \/\/ no harm done to headers<\/code><\/pre>\n<p><strong>Rule 2<\/strong>: Do not use fully qualified names everywhere.<br \/>\nReason: It draws people&#8217;s attention to the colon colon and confused people. <\/p>\n<pre><code class=\"language-c++\">\/\/ I think most people won&#039;t happy to review codes full of stds\nstd::string foo(std::map&lt;std::string, std::greater&lt;std::string&gt;, std::string&gt;&amp; table) {\n    std::string s1, s2;\n    std::cout &lt;&lt; \u201cEnter two strings:\u201d &lt;&lt; std::endl;\n    std::cin &gt;&gt; s1 &gt;&gt; s2;\n    std::map&lt;std::string, std::greater&lt;std::string&gt;, std::string&gt;::iterator it = table.find(s1);\n}<\/code><\/pre>\n<p><strong>Rule 3<\/strong>: Use specific using declarations.<br \/>\nReason: To solve rule 2, we write clear codes conviniently. We use local <code>using-declarations<\/code> instead of global ones. <\/p>\n<pre><code class=\"language-c++\">string foo(map&lt;string, greater&lt;string&gt;, string&gt;&amp; table) {\n    using std::string;\n    using std::map;\n    using std::greater;\n    using std::cout; using std::endl; using std::cin;\/\/ using console I\/O in the same line\n    string s1, s2;\n    cout &lt;&lt; \u201cEnter two strings:\u201d &lt;&lt; endl;\n    cin &gt;&gt; s1 &gt;&gt; s2;\n    map&lt;string, greater&lt;string&gt;, string&gt;::iterator it = table.find(s1);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce how to use &quot;using&quot; in C++, in other words, how to use an existing namespace. Conclusion As usual, I present the conclusion first. The followings are recommended ways which minimize name collision possibilities and maximize code clarity and writing convenience. See this post as well. For Headers Do&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-313","post","type-post","status-publish","format-standard","hentry","category-c","category-proglang"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=313"}],"version-history":[{"count":9,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/313\/revisions"}],"predecessor-version":[{"id":722,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/313\/revisions\/722"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}