Skip to content

Commit 0a054fc

Browse files
Clarifying the usage of 'using namespace' directives and 'using' declarationsin the NameSpace Section
1 parent a164d25 commit 0a054fc

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

cppguide.html

+24-3
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,6 @@ <h3 id="Names_and_Order_of_Includes">Names and Order of Includes</h3>
498498

499499
#include &lt;string&gt;
500500
#include &lt;vector&gt;
501-
502501
#include "base/basictypes.h"
503502
#include "foo/server/bar.h"
504503
#include "third_party/absl/flags/flag.h"
@@ -526,12 +525,34 @@ <h3 id="Namespaces">Namespaces</h3>
526525

527526
<p>With few exceptions, place code in a namespace. Namespaces
528527
should have unique names based on the project name, and possibly
529-
its path. Do not use <i>using-directives</i> (e.g.,
530-
<code>using namespace foo</code>). Do not use
528+
its path. Do not use <i>using-namespace</i>directives (e.g.,
529+
<code>using namespace foo;</code>). Do not use
531530
inline namespaces. For unnamed namespaces, see
532531
<a href="#Internal_Linkage">Internal Linkage</a>.
533532

533+
534534
</p><p class="definition"></p>
535+
<h4>Using Declaration:</h4>
536+
<p>Using declaration are allowed and can be used to refer to specific symbol from a namespace. For example</p>
537+
<pre><code>Using ::foo::bar;</code></pre>
538+
539+
<h4>Additional Note With an Example:</h4>
540+
541+
<h5>Using NameSpace Directives: </h5>
542+
<p>Using namespace directives brings the all the specified symbols/built-in-types from the specified NameSpace into the whatever current scope you are in, which leads to naming conflict and confusion(ambiguity). Therefore, they should be avoided.</p>
543+
544+
<p><strong>Example: </strong></p>
545+
<pre><code>//Avoid This Practice:
546+
using namespace std;
547+
std::cout,std::endl or somethinglike std::string st = "Hello";</code></pre>
548+
549+
<h5>Using Declarations:</h5>
550+
<p>Using Declarations allow you to bring namespace into current scope. This practice helps in avoiding the ambiguity(confusion) and which eventually keeps the code clear and manageable.</p>
551+
<p><strong>Example to Use:</strong></p>
552+
<pre><code>//Accepted
553+
using namespace std;
554+
cout,abs() or string str = "Hello,World";</code></pre>
555+
535556
<p>Namespaces subdivide the global scope
536557
into distinct, named scopes, and so are useful for preventing
537558
name collisions in the global scope.</p>

0 commit comments

Comments
 (0)